It must be a case of the fridays but looking for assistance in a simple script that runs on change of Field X (Option set) . It then looks at Field X for it to be a specific value and clears the value from Field Y (Lookup). I attempted this using business rule but our scenario is that Field Y will get repopulated we just want the value that's there to be cleared out for easy of entry.
If Field X value is 172960000 then clear the value in Field Y (Lookup). This is only firing on change of Field X.
*This post is locked for comments
Hi,
I have Implemented the same code on lead entity.
it worked fine, it removed value from currency lookup
function EscalationStatus() {
if (Xrm.Page.getControl("msdyn_ordertype") != null) {
if (Xrm.Page.getAttribute("msdyn_ordertype").getSelectedOption().value == "690970002") {
var EscalatedTeam = Xrm.Page.getAttribute("transactioncurrencyid");
if (EscalatedTeam != null) {
Xrm.Page.getAttribute("transactioncurrencyid").setValue(null);
}
}
}
}
Can we get more details on your both the fields.
screenshot will be better.
Hi Chris ,
Sorry it was my mistake - Try with this -
function EscalationStatus() {
if (Xrm.Page.getControl("lvp_escalationstatus") != null) {
if (Xrm.Page.getAttribute("lvp_escalationstatus").getValue() != null && Xrm.Page.getAttribute("lvp_escalationstatus").getValue() != undefined) {
if (Xrm.Page.getAttribute("lvp_escalationstatus").getSelectedOption().value == "172960000") {
var EscalatedTeam = Xrm.Page.getAttribute("tec_responsibleforteam");
if (EscalatedTeam != null) {
Xrm.Page.getAttribute("tec_responsibleforteam").setValue(null);
}
}
}
}
}
if above will not work try to replace " .getSelectedOption().value" with ".getValue()" in the highlighted line .
Thank for the brain power on this. Still getting an error if the option set goes to null.
TypeError: Cannot read property 'value' of null at EscalationStatus
Hi Chris,
Try with this I have added additional null check here - This should work without any error.
function EscalationStatus() { if (Xrm.Page.getControl("lvp_escalationstatus") != null && Xrm.Page.getAttribute("lvp_escalationstatus").getSelectedOption().value !=null) { if (Xrm.Page.getAttribute("lvp_escalationstatus").getSelectedOption().value == "172960000") { var EscalatedTeam = Xrm.Page.getAttribute("tec_responsibleforteam"); if (EscalatedTeam != null) { Xrm.Page.getAttribute("tec_responsibleforteam").setValue(null); } } } }
I implemented the script and it worked thank you. Only issue i saw is if the option set went from having a value to null i got an error. Is this an easy fix? Below is the code i used.
function EscalationStatus() { if (Xrm.Page.getControl("lvp_escalationstatus") != null) { if (Xrm.Page.getAttribute("lvp_escalationstatus").getSelectedOption().value == "172960000") { var EscalatedTeam = Xrm.Page.getAttribute("tec_responsibleforteam"); if (EscalatedTeam != null) { Xrm.Page.getAttribute("tec_responsibleforteam").setValue(null); } } } }
Hello Chris ,
Now I understood the issue you are facing .
This is the way business rules works in Dynamics CRM . When you create business rules in field on change it will be fire on change of the field as well as when you saved the page then internally its saved and open the record and fired business rules in onload of the form. That means business rules fired both onchange and onload event which causes the issue.
So in your case when you are changing the option set field its clear the lookup. Now if you again set any value in the lookup and it will saved but when it will again display the record the business rules found the optionset value and it will again clear the lookup value of that particular optionset onload of the form .
So you need to do it using javasccript which I have shared and make sure you only call onchange event and not onload event.
The business rule cleared out the lookup field and change of the option set. But once i saved the record, since the option set value didn't change, it cleared the value again. I just needed to clear it out once then allow a value to be saved in the lookup field.
Hello Chris,
Not sure what is the issue with business rules , make sure your business rules scope set to form. In addition could you please share screenshot of business rules.
As an alternate you can use Javascript code , here is the code , you need to just replace lookup name and option set name . Make sure you register the function in optionset onchange and as well as form onload event .
Hope this helps.
function SetLookupNullBasedOnOptionsetVal() { if (Xrm.Page.getControl("OptionSet Field Name") != null) { //Replace Optionset field Name if (Xrm.Page.getAttribute("OptionSet Field Name").getSelectedOption().value == "172960000 ") { //Replace Optionset field Name var lookupObject = Xrm.Page.getAttribute("Lookup Field Name"); // Replace lookup field name if (lookupObject != null) { Xrm.Page.getAttribute("Lookup Field Name").setValue(null);//Replace lookup field name } } } }
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,228 Super User 2024 Season 2
Martin Dráb 230,056 Most Valuable Professional
nmaenpaa 101,156