Forum Discussion
FICO
3 years agoCommunity Manager
How to access updated entity value in a JavaScript function?
I have written JS code to check on whether a user entered value is already in an index or not. However, I'm having a hard time passing in the modified entity value into the JS function. It seems the first time the function is called, it will always get the old value. Then, when I call the JS function the second time immediately after, the modified value gets passed in correctly.
I have tried two ways, one is to use var newName = vm.scenario.entities['newName'](); to retrieve the entity value in the JS function. The other way is to set a vdl-var first, then access that variable in JS function by using const newName = vm.vars.newName(); Both have resulted the same behavior.
Any suggestion on how to resolve this issue?
Thank you.
4 Replies
- FICOCommunity Manager
Hi,
I'm having trouble understanding where your issue becomes a problem. If the result is displayed then it will eventually resolve?
Is there some process that gets triggered from the result of this?
Joc
- FICOCommunity Manager
So at the end of that JS function, it will pop out a window to tell user if the entered value already exists. Right now, the first time user clicks the button, it is telling them the value already exists even though it is clearly a new value.
- FICOCommunity Manager
Here is an interesting finding. After I entered the value in the text field, if I don't click the action button right away and instead click somewhere outside the text field, that seems to trigger the Insight software to update the entity value and the JS code will perform correctly the first time. So I guess I'll need to tell user to click outside the text field before clicking the button? :-)
- FICOCommunity Manager
Here is a better solution after looking more at your VDL Language app. If I replace <vdl-field entity="newName"></vdl-field> with <input id="new_name" type="text" />, then add <vdl-action command="=document.getElementById('new_name').value"></vdl-action> and <vdl-action-update-entity entity="newName"></vdl-action-update-entity> before calling the JS function, the entity value will be updated even if I click the action button right after finishing typing in the text box. The only downside here is that the input text field is not automatically tied to the entity value so if I move to a different tab then come back, the content in the text field becomes empty again.