web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Need some help with this JavaScript

(0) ShareShare
ReportReport
Posted on by 803

I am wanting to create an Alert Box that shows up when a couple conditions are met. The function will fire off when the "Status" field changes. Here is the code I am using: 

function setFormAttributes(executionContext) {

    var formContext = executionContext.getFormContext();

    var lookupField = formContext.getAttribute("new_fieldone").getValue();
    var status = formContext.getAttribute("new_status").getValue();

    // create alert
    if (lookupField == "Bobs-Burgers" && status == "Current") {
		alert("Here is the alert));
	}
}

It's not working. I think the problem is that the "new_fieldone" is a lookup field, and the "new_status" field is an option set.
Any thoughts on how to get the values from these types of fields?

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi


    You can refer below blog on how to retrieve text using lookup and optionset.

    Lookup

    https://www.magnetismsolutions.com/blog/adammurchison/2017/09/20/how-to-get-and-set-a-lookup-field-using-javascript-in-dynamics-365

    remember using formcontext instead of xrm and remember put a checking for lookup field.

    Optionset

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/attributes/gettext

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    below is sample code

    function setFormAttributes(executionContext)

    {

       var formContext = executionContext.getFormContext();

       var lookupField = formContext.getAttribute("new_fieldone").getValue();

       var status = formContext.getAttribute("new_status").getText();

    if(lookupField != null)

    {

    // create alert

    if (lookupField[0].name == "Bobs-Burgers" && status == "Current")

    {

    alert("Here is the alert));

    }

    }

    }

    but i would recommend you to use value instead of text, in case in future the text will be changed.

    function setFormAttributes(executionContext)

    {

       var formContext = executionContext.getFormContext();

       var lookupField = formContext.getAttribute("new_fieldone").getValue();

       var status = formContext.getAttribute("new_status").getValue();

    if(lookupField != null)

    {

    // create alert

    if (lookupField[0].id == "guid" && status == "value")

    {

    alert("Here is the alert));

    }

    }

    }

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Ben,

    In addition to above, I would also suggest you to refer the below link as well. This has all the common JS syntax. Though it says 2011/2013, it should still work in other versions as well.

    garethtuckercrm.com/.../jscript-reference-for-microsoft-dynamics-crm-2011

    Hope this helps.

  • Ben T Profile Picture
    803 on at

    Ravi,

    Is it as simple as swtiching out formContext.getAttribute() where ever I see Xrm.page?

  • Ben T Profile Picture
    803 on at

    Wei,

    I was able to get the value of the lookup field, but still struggling with the option set. Here's my code:

    function setFormAttributes(executionContext) {

       var formContext = executionContext.getFormContext();

      var status = formContext.getAttribute("new_status").getText();

       // create alert

       alert(status);

    }

    I also tried using var status = formContext.getAttribute("new_status").getValue();

    Both are giving me the error "the given key in the dictionary is not present." I am certain the field is called "new_status," so not sure what the problem could be. It's just an option set with 5 options. Not a global option set either.

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    is "new_status" place inside form? if no , please do so. Do you have any workflows or plugins running? if yes, turn it off.

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Are you sure you are getting "the given key in the dictionary is not present."? Asking cause this is not a java script error but the C# error.

    mscrmuk.blogspot.com/.../the-given-key-was-not-present-in.html

    Can you share a screenshot please?

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Regarding switching Xrm.Page to formContext.getAttribute(). Yes, you just need to ensure that the execution context is passed as a parameter to the JS method and also in CRm you have selected the "Pass execution context" property checked.

    Refer this: community.dynamics.com/.../304981

    Hope this helps

  • Ben T Profile Picture
    803 on at

    Yes, it must be that we have a plug in that fires off when the status field is changed. However, we also want this javaScript alert to display when the status field is changed. Turning off the plugin isn't an option. Any simple ways to work around this?

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    the only way is to fix the plugin.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans