Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Setting the value of an Option set field in CRM 2016 with javascript

Posted on by Microsoft Employee

I am new to CRM so I can not solve the following.

I have a custom entity "Books". I have a field which is "numberofpages_prefix" integer, what I am trying to do is set the value of another option set field " complexity" with three options "easy" ,"medium" , "hard" depending on the number of pages entered. My event is set onChange and I keep getting the last else statement which is an error message instead of the options. I tried removing the else statement but I am still not getting the value set. Help would be very appreciated.
Here is my code:

function complexityChange ()
{
var pagesNumber = Xrm.Page.getAttribute("sonade_numberofpages").getValue();
var setComplexity = Xrm.Page.getAttribute("sonade_c");

if (pagesNumber > 0 && pagesNumber <= 99)
{
setComplexity.setValue("Easy");// also tried with the option value
}

if(pagesNumber > 99 && pagesNumber <= 299 )
{
setComplexity.setValue("Medium");// also tried with the option value
}

if(pagesNumber > 299)
{
setComplexity.setValue("Hard"); // also tried with the option value
}

else
{
Xrm.Page.getControl("sonade_numberofpages").setNotification("Pages number invalid");
}
}

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting the value of an Option set field in CRM 2016 with javascript

    Thank you Ravi, that was the issue.
    Very helpful :)

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Setting the value of an Option set field in CRM 2016 with javascript

    Hi,

    Ensure that the schema name of you roptionset field is correct and the value you enter is a numeric value i.e. instead of  976,820,000 try  976820000

    Xrm.Page.getAttribute("sonade_c").setValue(976820000);

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting the value of an Option set field in CRM 2016 with javascript

    I have tried with those aswell. In my case they are not 1,2,3 but seeing from a tutorial i tried to use 1,2,3  , then I checked the field options and used the ones you mentioned in my case 976,820,000 ,976,820,001, 976,820,002, still did not work

  • Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Setting the value of an Option set field in CRM 2016 with javascript

    Hello,

    To control OptionSet Values via code, you need to set the numeric value associated with the label.

    For example if you go the configuration page of your optionset field you will see that each label - easy, medium, hard will be associated with a unique numeric value (for example 1 for easy, 2 for medium and 3 for hard).

    If you want to set the value easy for the field you'd need to pass the numeric value as a parameter : Xrm.Page.getAttribute("new_optionfield").setValue(1)

    The reason why you need to set the numeric value and not the label is because this gives you the flexibility to change the label in time, without having to change the code as well.

    Regards,

    Radu

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!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans