admin管理员组

文章数量:1359930

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?

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?

Share Improve this question edited May 23, 2017 at 12:15 CommunityBot 11 silver badge asked Sep 5, 2013 at 10:56 KaniniKanini 1,9936 gold badges36 silver badges59 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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

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)

本文标签: Force a SaveForm in Dynamics CRM 2011 using JavaScriptStack Overflow