简体   繁体   中英

Force a SaveForm in Dynamics CRM 2011 using JavaScript

In CRM 2011, I want to be able to force a save of a form. My requirement is as below.

  1. When the user opens the form, I check if a particular field is empty or not.
  2. If it is empty, I want to force a save, thereby triggering a plugin which will set the field.

My current code is as below. This is in the FormOnLoad function which is associated with LoadForm.

if(checkfield == null){
  Namespace.Functions.Contact.FormOnSave();
}
else{
 // do nothing. 
}

Now, this fails with the following line

Can't execute code from a freed script.

I have no clue as to how to proceed further. As a temporary fix, what I have done is the following

if(checkfield == null){
  setTimeout('Namespace.Functions.Contact.FormOnSave()',4000);
}
else{
 // do nothing. 
}

Any advice on why this is happening and how can I fix it without the timeout would be really helpful?

I have had a look at this question and this , but in my case the form does get loaded or has it actually not yet loaded?

Either way, how can I force a save of a CRM form?

You can save the Form by using:

parent.Xrm.Page.data.entity.save();

But It will only trigger if

(Xrm.Page.data.entity.getIsDirty() == true)

However there is a workaround to set IsDirty property true. Run the Javascript function on PageLoad. Try to update the value of the field which you are planning to populate through plugin:

For eg

function OnPageLoad()
{
    var yourField = Xrm.Page.getAttribute("new_yourfield").getValue();
    if(yourField == null)
    {
        parent.Xrm.Page.getAttribute("new_yourfield").setValue("any value");
        parent.Xrm.Page.getAttribute("new_yourfield").setSubmitMode("always");
        parent.Xrm.Page.data.entity.save();
    }
}

You could skip all of this mumble jumbo with loading your form twice with Javascript by creating a plugin registered on the read or the entity. (Or better yet and if possible, on the Create of the entity)

The Plugin would just check for the field, and if it's empty, perform whatever logic you need it to to update the field, then save it, and return the updated field (or possibly just populate the value and don't update the database until the user performs the save)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM