Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Showing alert based on related entity's field value

(0) ShareShare
ReportReport
Posted on by 1,328

Upon loading a Quote form, I want to show an alert based on the value of a field called "new_stage" from the related opportunity. I used CRM REST Builder for retrieveRecord and then added an if statement to check this field and show the alert. Here is the error. I'm new to javascript so I'm having trouble with my code. Any help would be appreciated.

3301.Capture.PNG

        function retrieveStage(opportunityId)
        {
            SDK.REST.retrieveRecord(
                opportunityId, 
                "Opportunity",
                "opportunity_quotes/new_stage", 'opportunity_quotes',
                function (result) {
                    var stage = result.opportunity_quotes.new_stage;
                }, function(error) {
                    Xrm.Utility.alertDialog(error.message);
                });

            if (stage != null && stage != 1) {
                alert("This is my test message.");
            }
        }



*This post is locked for comments

  • epark06 Profile Picture
    1,328 on at
    RE: Showing alert based on related entity's field value

    [quote user="Goutam Das"]

    Try with this-

    function checkstage(opportunity) {

       debugger;

       var stage =

    opportunity.new_stage.Value;

       if (stage == 2) {

           alert("This is a test message.");

    }

    }

    If above will not work please refer below

    social.microsoft.com/.../sdkrestretrieverecord-optionset-value

    [/quote]

    Thanks, this works!

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Showing alert based on related entity's field value

    Try with this-

    function checkstage(opportunity) {

       debugger;

       var stage =

    opportunity.new_stage.Value;

       if (stage == 2) {

           alert("This is a test message.");

    }

    }

    If above will not work please refer below

    social.microsoft.com/.../sdkrestretrieverecord-optionset-value

  • Verified answer
    epark06 Profile Picture
    1,328 on at
    RE: Showing alert based on related entity's field value

    FINALLY! I got it to work by not using the var "stage" and just doing this. Thank you all for your help!

               function checkstage(opportunity) {

                   var stage = opportunity.new_stage;

                   if (opportunity.new_stage.Value == 2) {

                       alert("This is a test message.");

                   }

               }

  • epark06 Profile Picture
    1,328 on at
    RE: Showing alert based on related entity's field value

    Looks like my variable "stage" is also undefined...

    52520.Capture.PNG

    Latest code:

    function retrieveStage(opportunityId) {
        var opportunityId = Xrm.Page.getAttribute("opportunityid").getValue()[0].id;
        var EntitySchemaName = "Opportunity";
    
        if (opportunityId != null) {
            SDK.REST.retrieveRecord(
            opportunityId,
            EntitySchemaName,
            "new_stage", null,
            checkstage, errorHandler );
        }
    }
    function checkstage(opportunity) {
        debugger;
        var stage = opportunity.new_stage;
        if (stage == 2) {
            alert("This is a test message.");
    
    }
    }
    
    function errorHandler(error) {
        alert(error.message);
    }
  • epark06 Profile Picture
    1,328 on at
    RE: Showing alert based on related entity's field value

    Tried "if (stage == 2)" and now there is never an alert, and still get the SyntaxError.

    }
    function checkstage(opportunity) {
        debugger;
        var stage = opportunity.new_stage;
        if (stage == 2) {
            alert("This is a test message.");
    
    }

    51662.Capture.PNG

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Showing alert based on related entity's field value

    Hello ,

    You are getting message every time as in the IF condition you are setting the value instead of check value 2 , its should be "   if (stage == 2)  " instead of  "   if (stage = 2) " .

    Here is corrected code. Hope this helps.

      function retrieveStage(opportunityId) {
                var opportunityId = Xrm.Page.getAttribute("opportunityid").getValue()[0].id;
                var EntitySchemaName = "Opportunity";
    
                if (opportunityId != null) {
                    SDK.REST.retrieveRecord(
                    opportunityId,
                    EntitySchemaName,
                    "new_stage", null,
                    checkstage, errorHandler);
                }
            }
            function checkstage(opportunity) {
                var stage = opportunity.new_stage;
                if (stage == 2) {
                    alert("This is my test message.");
                }
            }
    
            function errorHandler(error) {
                alert(error.message);
            }


  • epark06 Profile Picture
    1,328 on at
    RE: Showing alert based on related entity's field value

    Ok, I narrowed it down in the debugger. It is giving a SyntaxError for the != in the filter. Same error for == and =. Any help?

    4011.Capture.PNG

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Showing alert based on related entity's field value

    Then I'm afraid you will have to learn how to troubleshoot the code - blogs.msdn.microsoft.com/.../debugging-custom-javascript-code-in-crm-using-browser-developer-tools

    Good luck in this.

  • epark06 Profile Picture
    1,328 on at
    RE: Showing alert based on related entity's field value

    New code with suggested changes. It is still firing alert every time :(

    function retrieveStage(opportunityId) {
        var opportunityId = Xrm.Page.getAttribute("opportunityid").getValue()[0].id;
        var EntitySchemaName = "Opportunity";
    
        if (opportunityId != null) {
            SDK.REST.retrieveRecord(
            opportunityId,
            EntitySchemaName,
            "new_stage", null,
            checkstage, errorHandler );
        }
    }
    function checkstage(opportunity) {
        var stage = opportunity.new_stage;
        if (stage = 2) {
            alert("This is my test message.");
        }
    }
    
    function errorHandler(error) {
        alert(error.message);
    }
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Showing alert based on related entity's field value

    Ok. It's fun that I haven't seen it but here is why your code doesn't work - opportunityId and opportunityID are different variables for JS so to make your code work change

       if (opportunityID != null) {

           SDK.REST.retrieveRecord(

           opportunityID,

           EntitySchemaName,

           "new_stage", null,

           checkstage, errorHandler );

       }

    to

       if (opportunityId != null) {

           SDK.REST.retrieveRecord(

           opportunityId,

           EntitySchemaName,

           "new_stage", null,

           checkstage, errorHandler );

       }

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans