Forum Discussion

RaptureLazarus's avatar
5 years ago
Solved

Client side Form function and script not evaluating correctly.

Good day all.

TL;DR: I am having trouble getting an onLoad method to reliably print an evaluated value to a field.

The form is as follows: 

Stock message contains: [embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:d909f647-ad8c-4825-983b-76cb1bd8d97c:type=text&text=%3Cspan%20id%3D%27stock_msg%27%3E%3C%2Fspan%3E]

I have a javascript onLoad method that compares the minimum and current stock:

[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:6207aced-4ad7-42a1-8524-f7d5d4e5bb1b:type=javascript&text=%2F%2F%20Create%20min%20and%20current%20stock%20variables%0D%0Avar%20minStock%20%3D%20document.thisItem.getProperty%28%22_minimum_stock%22%29%3B%0D%0Avar%20currStock%20%3D%20document.thisItem.getProperty%28%22_current_stock%22%29%3B%0D%0A%0D%0A%2F%2F%20Bring%20in%20HTML%20field%20span%0D%0Avar%20field%20%3D%20document.getElementById%28%22stock_msg%22%29%3B%0D%0A%2F%2F%20Create%20stockMsgValue%20variable%0D%0Avar%20stockMsgValue%20%3D%20null%3B%0D%0A%0D%0A%2F%2F%20if%20current%20stock%20is%20less%20than%20or%20equals%20to%20minimum%20stock%0D%0Aif%28currStock%20%3C%3D%20minStock%29%0D%0A%7B%0D%0A%20%20%20%20stockMsgValue%20%3D%20%22%3Cp%20style%3D%27color%3A%20red%27%3ENot%20enough%20Stock%21%3C%2Fp%3E%22%3B%0D%0A%7D%20else%20%7B%0D%0A%20%20%20%20stockMsgValue%20%3D%20%22%3Cp%20style%3D%27color%3A%20green%27%3EStock%20levels%20look%20good%3C%2Fp%3E%22%3B%0D%0A%7D%0D%0A%0D%0A%2F%2F%20Write%20to%20HTML%20field%20span%0D%0Afield.innerHTML%20%2B%3D%20stockMsgValue%3B]

I did something similar with Stock message 2, but included the javascript in the HTML field directly:

[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:8db9994b-faa4-454c-9a4a-dfaec63bfdd3:type=html&text=%3Cdiv%20id%3D%22Message%22%3E%3C%2Fdiv%3E%0D%0A%3Cdiv%20id%3D%22min%22%3E%3C%2Fdiv%3E%0D%0A%3Cdiv%20id%3D%22curr%22%3E%3C%2Fdiv%3E%0D%0A%0D%0A%3Cscript%3E%0D%0Avar%20divText%20%3D%20document.getElementById%28%22Message%22%29%3B%0D%0Avar%20divCurr%20%3D%20document.getElementById%28%22curr%22%29%3B%0D%0Avar%20divMin%20%3D%20document.getElementById%28%22min%22%29%3B%0D%0Avar%20currStock%20%3D%20document.thisItem.getProperty%28%22_current_stock%22%29%3B%0D%0Avar%20minStock%20%3D%20document.thisItem.getProperty%28%22_minimum_stock%22%29%3B%0D%0A%0D%0Aif%28currStock%20%3C%3D%20minStock%20%29%20%7B%0D%0A%20%20%20%20var%20textVal%20%3D%20%22%3Cspan%20style%3D%27color%3A%20red%27%3B%3EInsufficient%3C%2Fspan%3E%22%3B%0D%0A%7D%0D%0Aelse%20%7B%0D%0A%20%20%20%20var%20textVal%20%3D%20%22%3Cspan%20style%3D%27color%3A%20green%27%3B%3ESufficient%3C%2Fspan%3E%22%3B%0D%0A%7D%0D%0A%0D%0AdivText.innerHTML%20%2B%3D%20textVal%3B%0D%0AdivMin.innerHTML%20%2B%3D%20%22Minimum%20stock%3A%20%22%20%2BminStock%3B%0D%0AdivCurr.innerHTML%20%2B%3D%20%22Current%20stock%3A%20%22%20%2B%20currStock%3B%0D%0A%0D%0A%3C%2Fscript%3E]

The problem is that it works for a little bit, and then the evaluation does not reflect the correct values.

Some examples:

Any ideas?

Thanks

  • AngelaIp's avatar
    AngelaIp
    5 years ago

    Are your fields "string" or "integer" properties? According to your debugger, they look like strings! In this case greater and less may calculate based on wrong values.

    Try to parse the value  --> 

    parseInt(minStock, 10)

    And maybe use an onFormPopulated event instead of onLoad, when the fields shall also update when user edit the item.

8 Replies

  • In general I would use an "onLoad" Method instead of a "Form" script. Simply because Form script are easy to forget and hard to edit. For the Aras Update team a Method is easier to find and update than custom code somewhere hidden in the Form.

    Of course both variants work and sometimes you have to use Form scripts due to technical reasons. "Form" scripts are loaded before (!) your "onLoad" Method.

    I don´t see an obvious bug in your code. Maybe you just have timing issue. Instead of calling the code directly with the <script> tag, wrap it into a function:

    <script>
    function dosomthing()
    {
        // place your custom code here
    }

    dosometing();
    </script>

    • AngelaIp's avatar
      AngelaIp
      Ideator I

      If it doesn´t work, add the "debugger" statement to your script and check with the browser debugger what happens on runtime. Than you should be able to find your error easily!

      • RaptureLazarus's avatar
        RaptureLazarus
        Ideator I

        HI Angela, thanks for your suggestion, however I am still stuck.

        I have added the debugger and function call on both the onLoad method and HTML field variants, but I'm still getting the same result.

        I even separated the if function as follows, to try and get a different result:
        [embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:cf52f2b7-d8a6-474d-96b9-c6cd5d6029a4:type=text&text=if%28currStock%20%3C%20minStock%29%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20field.innerHTML%20%2B%3D%20%22%3Cp%20style%3D%27color%3A%20red%27%3ENot%20enough%20Stock%21%3C%2Fp%3E%22%3B%0D%0A%20%20%20%20%20%20%20%20return%3B%0D%0A%20%20%20%20%7D%20%0D%0A%20%20%20%20if%28currStock%20%3E%20minStock%29%7B%0D%0A%20%20%20%20%20%20%20%20field.innerHTML%20%2B%3D%20%22%3Cp%20style%3D%27color%3A%20green%27%3EStock%20levels%20look%20good%3C%2Fp%3E%22%3B%0D%0A%20%20%20%20%20%20%20%20return%3B%0D%0A%20%20%20%20%7D]

        The second if statement is triggered every time.