Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

How to set optionset value as selected using JS

Posted on by 10

Hello all,

This is my issue : 

When a user selects an option for which he does not have the authorizations, an error message appears as an alert (it's okay up here).

Then, I would like another optionset to be selected to replace the one the user has chosen. 

Optionset-issue.jpg

This is the codes I have tried but that didn't work for me : 

Xrm.Page.getAttribute("NameOfMyField").setValue(null); //Here I have even tried to replace the null value by the one of another optionset like ("860330002") but unsuccessfull

I've tried as well : Xrm.Page.Data.Entity.Attributes.Get("nameofmyfield").setValue(null); //Did not work for me as well

Need support ASAP to solve an issue for a customer. 

Thanks 

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: How to set optionset value as selected using JS

    Hi,

    So you need to first set null value then show warning message.

  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: How to set optionset value as selected using JS

    Hi RaphaelMG,

    write the below lines to set the value:

    formContext.getAttribute("NameOfMyField").setSubmitMode("always");//details

    formContext.getAttribute("NameOfMyField").setValue(value);// details.

    formContext.data.save().then(successCallback, errorCallback);//details

    Abdul Wahab
    Power Platform/Customer Engagement Developer/Lead/Solution Architecture/Project Manager
    Direct/WhatsApp:+923323281237
    E-mail: abdulwahabubit@outlook.com
    Skype: abdul.wahabubit

  • RaphaelMG Profile Picture
    RaphaelMG 10 on at
    RE: How to set optionset value as selected using JS

    Hi Bipin Kumar

    Thanks for your update. I've tried using setValue(-1) as you suggested me but it did not work.

    I found out that by removing the alert() function from the code (which warns the user that he doesn't have the right to make this change),  everything works fine.

    I don't understand how this could block the normal execution of the function shared above : Xrm.Page.Data.Entity.Attributes.Get("nameofmyfield").setValue(null);

    Since there was an urgency to unblock the client and it worked, I can look into your recommendation to use formcontext and do some testing, Since the user must have a warning message.

    Thanks for your feedback

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: How to set optionset value as selected using JS

    Hi,

    Can you please try setting -1 value as below

    Xrm.Page.getAttribute("NameOfMyField").setValue(-1);

    Since you are on CRM Online then why don't you use formcontext instead of Xrm.Page because Xrm.Page is deprecated.

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans