Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Javascript to Clear Value on Change

Posted on by 70

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

  • Suggested answer
    Friyank Profile Picture
    Friyank 944 on at
    RE: Javascript to Clear Value on Change

    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.

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Javascript to Clear Value on Change

    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 .

  • Chris Holst Profile Picture
    Chris Holst 70 on at
    RE: Javascript to Clear Value on Change

    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

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Javascript to Clear Value on Change

    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);
                        }
                    }
                }
            }


  • Chris Holst Profile Picture
    Chris Holst 70 on at
    RE: Javascript to Clear Value on Change

    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);
                }
            }
        }
    }
  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Javascript to Clear Value on Change

    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.

  • Chris Holst Profile Picture
    Chris Holst 70 on at
    RE: Javascript to Clear Value on Change

    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.

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Javascript to Clear Value on Change

    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
                }
            }
        }
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,228 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,056 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans