Answered by:
Lead OnSave cancels jscript

Question
-
On a Lead Entity, we have added lookup fields for Existing Customer and Existing Contact. Jscript runs onchange for Existing Contact that changes Last Name field to "not required". This works fine until I try and Save, the field reverts back to being required. I tried setSubmitMode("always"), but it did not work. I am sure it is somehow related to Qualifying the Lead, but cannot seem to find a way around this. Here is the code:
function ExistingContact_onchange()
{
var ExistingContact = Xrm.Page.getAttribute("synact_existingcontactid").getValue();
var FirstName = Xrm.Page.getAttribute("firstname");
var LastName = Xrm.Page.getAttribute("lastname");if(ExistingContact != null)
{
FirstName.setValue(null);
FirstName.setRequiredLevel("none");
Xrm.Page.ui.controls.get("firstname").setDisabled(true);
LastName.setValue(null);
LastName.setRequiredLevel("none");
Xrm.Page.ui.controls.get("lastname").setDisabled(true);LastName.setSubmitMode("always");
}
}
Ken Compter
Monday, May 13, 2013 5:04 PM
Answers
-
On the field properties - make the first and last name fields business required.
Now without any code being applied they will be mandatory.
Use the code below - ExistingContact_onchange goes with that field's OnChange event and OnLoad goes with the form's OnLoad event.
So now when the form opens when creating a record, if the existing contact field is null (not having been populating via a mapping) the first and last name fields will be required. As you populate or remove the value from the existing contact field, the requirement level on those fields should change accordingly.
Save and close
Now when you reopen the form the if the existing contact field is null, (the code fires from our OnLoad event) the 2 fields will still be required, however if the field does contain a value, those 2 fields should not be required.
The key is that setting the requirement level on a field using JavaScript only applies to the time that record is open, once you close the record and reopen the requirement level goes back to whatever the default is at the field level unless you run JavaScript again when the form loads to switch the requirement level based on whatever your criteria is - generally this is the same criteria as whatever is changing when you were first setting the values. That is why I broke out the logic into a separate method because it is getting called in 2 spots. I added some logic to ensure the OnLoad event only fires when the form can be updated (create and update mode).
setSubmitMode only applies when you are changing the value of a read-only field. By default CRM does not submit the values in read-only fields (saves bandwidth etc...) unless you explicitly tell it to. Logic being most times read-only fields are just that - read-only - no need to resubmit a value that doesn't change. But when we change it using JavaScript - that is when we need to tell CRM to go ahead and submit the value.
function ExistingContact_onchange() { MakeRequired(); } function OnLoad() { var formType = Xrm.Page.ui.getFormType(); if (formType == 1 || formType == 2) { MakeRequired(); } } function MakeRequired() { var ExistingContact = Xrm.Page.getAttribute("synact_existingcontactid").getValue(); var FirstName = Xrm.Page.getAttribute("firstname"); var LastName = Xrm.Page.getAttribute("lastname"); if (ExistingContact != null) { FirstName.setValue(null); FirstName.setRequiredLevel("none"); Xrm.Page.ui.controls.get("firstname").setDisabled(true); LastName.setValue(null); LastName.setRequiredLevel("none"); Xrm.Page.ui.controls.get("lastname").setDisabled(true); } else { FirstName.setRequiredLevel("required"); Xrm.Page.ui.controls.get("firstname").setDisabled(false); LastName.setRequiredLevel("required"); Xrm.Page.ui.controls.get("lastname").setDisabled(false); } }
Good luck!
Jason Lattimer
My Blog - Follow me on Twitter - LinkedIn- Proposed as answer by JLattimerMVP, Moderator Tuesday, May 14, 2013 2:18 AM
- Marked as answer by Payman BiukaghazadehEditor Thursday, May 16, 2013 12:17 PM
Tuesday, May 14, 2013 2:18 AMModerator
All replies
-
Hi,
you need to add your code also inside the onload event (at least the part to set the requirement level), this because the requirement level is a property of the field, not a property of the current record.
My blog: www.crmanswers.net
- Edited by Guido PreiteMVP Monday, May 13, 2013 5:38 PM
- Proposed as answer by JLattimerMVP, Moderator Monday, May 13, 2013 5:40 PM
Monday, May 13, 2013 5:38 PM -
I added it to the onsave event, no difference.
Ken Compter
Monday, May 13, 2013 7:17 PM -
Try the OnLoad event :)
Jason Lattimer
My Blog - Follow me on Twitter - LinkedInMonday, May 13, 2013 7:26 PMModerator -
If I put into the Onload event it would make the field Not Required, but I do want it required unless the Existing Contact field contains data. So I tried making the field not required, then tried to make required with Onsave event and setSubmitMode("always"). . When you hit save, it changes the field to required, but then when the form refreshes, it becomes Not Required.
It seems that it returns the required field back to the original value.
Ken Compter
Monday, May 13, 2013 10:35 PM -
On the field properties - make the first and last name fields business required.
Now without any code being applied they will be mandatory.
Use the code below - ExistingContact_onchange goes with that field's OnChange event and OnLoad goes with the form's OnLoad event.
So now when the form opens when creating a record, if the existing contact field is null (not having been populating via a mapping) the first and last name fields will be required. As you populate or remove the value from the existing contact field, the requirement level on those fields should change accordingly.
Save and close
Now when you reopen the form the if the existing contact field is null, (the code fires from our OnLoad event) the 2 fields will still be required, however if the field does contain a value, those 2 fields should not be required.
The key is that setting the requirement level on a field using JavaScript only applies to the time that record is open, once you close the record and reopen the requirement level goes back to whatever the default is at the field level unless you run JavaScript again when the form loads to switch the requirement level based on whatever your criteria is - generally this is the same criteria as whatever is changing when you were first setting the values. That is why I broke out the logic into a separate method because it is getting called in 2 spots. I added some logic to ensure the OnLoad event only fires when the form can be updated (create and update mode).
setSubmitMode only applies when you are changing the value of a read-only field. By default CRM does not submit the values in read-only fields (saves bandwidth etc...) unless you explicitly tell it to. Logic being most times read-only fields are just that - read-only - no need to resubmit a value that doesn't change. But when we change it using JavaScript - that is when we need to tell CRM to go ahead and submit the value.
function ExistingContact_onchange() { MakeRequired(); } function OnLoad() { var formType = Xrm.Page.ui.getFormType(); if (formType == 1 || formType == 2) { MakeRequired(); } } function MakeRequired() { var ExistingContact = Xrm.Page.getAttribute("synact_existingcontactid").getValue(); var FirstName = Xrm.Page.getAttribute("firstname"); var LastName = Xrm.Page.getAttribute("lastname"); if (ExistingContact != null) { FirstName.setValue(null); FirstName.setRequiredLevel("none"); Xrm.Page.ui.controls.get("firstname").setDisabled(true); LastName.setValue(null); LastName.setRequiredLevel("none"); Xrm.Page.ui.controls.get("lastname").setDisabled(true); } else { FirstName.setRequiredLevel("required"); Xrm.Page.ui.controls.get("firstname").setDisabled(false); LastName.setRequiredLevel("required"); Xrm.Page.ui.controls.get("lastname").setDisabled(false); } }
Good luck!
Jason Lattimer
My Blog - Follow me on Twitter - LinkedIn- Proposed as answer by JLattimerMVP, Moderator Tuesday, May 14, 2013 2:18 AM
- Marked as answer by Payman BiukaghazadehEditor Thursday, May 16, 2013 12:17 PM
Tuesday, May 14, 2013 2:18 AMModerator -
Hi,
If you don't add this code at OnLoad event of the form then how you will old form value what you saved last time.
So add you code at the OnLoad event and run the below code if fromType is not equal to "New" i.e.
if(Xrm.Page.ui.getFormType()!=1){
var ExistingContact = Xrm.Page.getAttribute("synact_existingcontactid").getValue();
if(ExistingContact != null)
{
FirstName.setValue(null);
FirstName.setRequiredLevel("none");
Xrm.Page.ui.controls.get("firstname").setDisabled(true);
LastName.setValue(null);
LastName.setRequiredLevel("none");
Xrm.Page.ui.controls.get("lastname").setDisabled(true);LastName.setSubmitMode("always");
}}
Please let me know if you are still facing same issue.
Thanks, Prakash Omer Ignify | Email: prakash.omer@hotmail.com
- Proposed as answer by Prakash Omer Tuesday, May 14, 2013 7:37 AM
Tuesday, May 14, 2013 7:35 AM