Reputation: 79
I've created some code for a custom entity involving dynamically displayed questions and the ability to answer them. I'm working on being able to display previously answered questions, along with the ability to update answers. To do so, I have certain functions fire onLoad of my custom entity form, and another fire onSave.
My trouble is that my onLoad code runs every time a save happens, effectively erasing the output of the onSave code. I only want my onLoad code to run the first time the form is loaded. Otherwise, the code I want to fire onSave should handle everything else.
One way I tried to solve this was using Xrm.Page.ui.getFormType();
This returns a value of 1 or 2, with 1 corresponding to the record being created, and 2 corresponding to the record being updated. This worked for one scenario -- I didn't want my onLoad code to run when a fresh record for the custom entity was being created, and getFormType()
solved this. BUT when I saved the record, the return value changed, and caused my onLoad code to run OnSave, which messed things up.
Any ideas on how I would go about solving this? I can forsee something like getFormType()
being useful, but I'm at a bit of a loss on how to go about things. Thanks!
Upvotes: 0
Views: 2785
Reputation: 2990
Add an onChange event handler to the modifiedon attribute, this value would only ever change after a record is saved, you can have a global parameter and set its value and use its value to stop the onLoad from re-firing again.
var canRunOnLoadCode = true;
//form onLoad function
function onLoad() {
if(!canRunOnLoadCode) return;
//on load code below
}
//register the below function on change of modified on
function afterOnSave(){
canRunOnLoadCode = false;
}
Upvotes: 1