Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Javascript, OnChange isn't triggering until second selection?

Posted on by 2

Let me preface that I have very little knowledge of what I'm doing, I've mostly googled to get this far.

I have an OptionSet (Yes/No) field on my form. 

I've set a OnChange Event for the field it to trigger a Javascript function.

The Javascript function checks if the value of the field is Yes, and if so it makes a WebResource visible.

It "sort of" works, but it requires me to toggle away from Yes before the WebResource is shown.

I.E. If the field is defaulted to [ -- Select -- and I choose [ Yes , nothing happens. But as soon as I toggle from [ Yes to any other value (No or back to default --Select--), then the function triggers and the WebResource is shown. So apparently OnChange is seeing the previous value when I change the field??? I can't seem to make head-or-tails of what actually causes the OnChange to trigger.

Below is a sample of my code:

function SetInvisibleWebResource(executionContext) {
    var formContext = executionContext.getFormContext();    
    var Q1 = formContext.getAttribute("crc7b_segment1").getValue();

     if (Q1 !== null){
             if (Q1 == 260830000){ 
            formContext.getControl("WebResource_Segmentation_1").setVisible(true);
        }}
}

 260830000 is the value of Yes

Can anyone help point out what I'm doing wrong?

  • Verified answer
    Cayshin Profile Picture
    Cayshin 2 on at
    RE: Javascript, OnChange isn't triggering until second selection?

    Ok, so I figured out a messy way that "works". I had stop using formContext and go back to Xrm.page (I don't know enough about CRM to know what the difference is or why/when to use one over the other), Then I had to create a duplicate of my below javascript.

    function SetInvisibleWebResource() {
    
        if (Xrm.Page.getAttribute("crc7b_segment1").getValue() == 260830000 && Xrm.Page.getControl("crc7b_segment1").getVisible() == true){
            Xrm.Page.getControl("crc7b_segment2").setVisible(false);
            Xrm.Page.getControl("crc7b_segment3").setVisible(false);
            Xrm.Page.getControl("crc7b_segment4").setVisible(false);
            Xrm.Page.getControl("crc7b_segment5").setVisible(false);
            Xrm.Page.getAttribute("tcg_segmentationmodel").setValue(260830000);
            Xrm.Page.getAttribute("tcg_segmentationmodel").fireOnChange();
        }
        else if (Xrm.Page.getAttribute("crc7b_segment1").getValue() == 260830001 && Xrm.Page.getControl("crc7b_segment1").getVisible() == true && Xrm.Page.getControl("crc7b_segment2").getVisible() == false){
            Xrm.Page.getControl("crc7b_segment2").setVisible(true);
            Xrm.Page.getAttribute("crc7b_segment2").setValue(null);
        }
        
        if (Xrm.Page.getAttribute("crc7b_segment2").getValue() == 260830000 && Xrm.Page.getControl("crc7b_segment2").getVisible() == true){
            Xrm.Page.getControl("crc7b_segment3").setVisible(false);
            Xrm.Page.getControl("crc7b_segment4").setVisible(false);
            Xrm.Page.getControl("crc7b_segment5").setVisible(false);
            Xrm.Page.getAttribute("tcg_segmentationmodel").setValue(799640000);
            Xrm.Page.getAttribute("tcg_segmentationmodel").fireOnChange();
        }
        else if (Xrm.Page.getAttribute("crc7b_segment2").getValue() == 260830001 && Xrm.Page.getControl("crc7b_segment2").getVisible() == true && Xrm.Page.getControl("crc7b_segment3").getVisible() == false){
            Xrm.Page.getControl("crc7b_segment3").setVisible(true);
            Xrm.Page.getAttribute("crc7b_segment3").setValue(null);
        }
        
        if (Xrm.Page.getAttribute("crc7b_segment3").getValue() == 260830000 && Xrm.Page.getControl("crc7b_segment3").getVisible() == true){
            Xrm.Page.getControl("crc7b_segment4").setVisible(false);
            Xrm.Page.getControl("crc7b_segment5").setVisible(false);
            Xrm.Page.getAttribute("tcg_segmentationmodel").setValue(799640001);
            Xrm.Page.getAttribute("tcg_segmentationmodel").fireOnChange();
        }
        else if (Xrm.Page.getAttribute("crc7b_segment3").getValue() == 260830001 && Xrm.Page.getControl("crc7b_segment3").getVisible() == true && Xrm.Page.getControl("crc7b_segment4").getVisible() == false){
            Xrm.Page.getControl("crc7b_segment4").setVisible(true);
            Xrm.Page.getAttribute("crc7b_segment4").setValue(null);
        }
        
        if (Xrm.Page.getAttribute("crc7b_segment4").getValue() == 260830000 && Xrm.Page.getControl("crc7b_segment4").getVisible() == true){
            Xrm.Page.getControl("crc7b_segment5").setVisible(false);
            Xrm.Page.getAttribute("tcg_segmentationmodel").setValue(799640002);
            Xrm.Page.getAttribute("tcg_segmentationmodel").fireOnChange();
        }
        else if (Xrm.Page.getAttribute("crc7b_segment4").getValue() == 260830001 && Xrm.Page.getControl("crc7b_segment4").getVisible() == true && Xrm.Page.getControl("crc7b_segment5").getVisible() == false){
            Xrm.Page.getControl("crc7b_segment5").setVisible(true);
            Xrm.Page.getAttribute("crc7b_segment5").setValue(null);
        }
        
        if (Xrm.Page.getAttribute("crc7b_segment5").getValue() == 260830000 && Xrm.Page.getControl("crc7b_segment5").getVisible() == true){
            Xrm.Page.getAttribute("tcg_segmentationmodel").setValue(799640003);
            Xrm.Page.getAttribute("tcg_segmentationmodel").fireOnChange();
        }
        else if (Xrm.Page.getAttribute("crc7b_segment2").getValue() == 260830001 && Xrm.Page.getControl("crc7b_segment5").getVisible() == true){
            Xrm.Page.getAttribute("tcg_segmentationmodel").setValue(799640004);
            Xrm.Page.getAttribute("tcg_segmentationmodel").fireOnChange();
        }
        
            }
        

    So now on the Events for segment_1, segment_2., etc... controls I've added both javascripts.

    Screenshot-2021_2D00_06_2D00_30-111148.png

    I'm sure this is the completely wrong way to do it, but it works so I'm happy =).

  • Cayshin Profile Picture
    Cayshin 2 on at
    RE: Javascript, OnChange isn't triggering until second selection?

    Hi Mehdi,

    I'm sorry, I'm very new to customizing CRM forms and am ignorant of most of the terms and what they mean/reference. When you say "actual control" what's that?

    Would the control be my OptionSet field with the Yes/No selections? If so, I've tested it using the Two Options field type but ran into the same issue.

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,198 User Group Leader on at
    RE: Javascript, OnChange isn't triggering until second selection?

    Hi, 

    I think that the issue is not in your JS code..can you please change your actual control with another one ? 

  • RE: Javascript, OnChange isn't triggering until second selection?

    Hello, try this:

    function SetInvisibleWebResource(executionContext) {

       var formContext = executionContext.getFormContext();

       // OnChange must execute this even if the field is in "-- Select --" and you change to "Yes"

       formContext.getAttribute("crc7b_segment1").addOnChange(SetInvisibleWebResource_aux);

    }

    function SetInvisibleWebResource_aux(formContext) {

       var Q1 = formContext.getAttribute("crc7b_segment1").getValue();

       if (Q1 !== null){

                if (Q1 == 260830000){

               formContext.getControl("WebResource_Segmentation_1").setVisible(true);

           }}

    }

    I hope this work!

    Thanks!

    Community Support Team - Esteban

    If this Post helps, then please consider Accept as solution to help the other members find it more quickly.

  • Cayshin Profile Picture
    Cayshin 2 on at
    RE: Javascript, OnChange isn't triggering until second selection?

    Hi Andrew,

    Thanks for the suggestion; however, the same issue persists.

    Here's a video of what's happening.

    [View:/cfs-file/__key/communityserver-discussions-components-files/117/Javascript-Issue.mp4:426:320]

    Notice how the URL links only appear after I select away from [ Yes ].

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Javascript, OnChange isn't triggering until second selection?

    Hello,

    Try to use the following code:

    function SetInvisibleWebResource(executionContext) {
    	var formContext = executionContext.getFormContext();    
    	var Q1 = formContext.getAttribute("crc7b_segment1").getValue();
    
    	formContext.getControl("WebResource_Segmentation_1").setVisible(Q1 === 260830000);
    }

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,537 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,520 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans