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
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.