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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Radu Chiribelea Profile Picture
    6,667 on at

    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

  • Community Member Profile Picture
    on at

    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

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

    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
    on at

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

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans