Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Answered

Adding options to my Option Set.

(0) ShareShare
ReportReport
Posted on by 106

Hi all, I have a form(Entity Y) on save of which values from an entity X come and sit in entity Y, note that i need to lookup the record and save to do this(My implementation).
Now, i have an empty Option Set field, which should be populated with values coming from Entity X.
How do i go about achieving this ?

  • LeoAlt Profile Picture
    16,331 Moderator on at
    RE: Adding options to my Option Set.

    Hi KashyapT,

    That because the js code only choose the label but not value.

    Here is a blog for you to resolve this issue.

    blogs.msdn.microsoft.com/.../

    Hope it helps.

    Best Regards,

    Leo

  • KashyapT Profile Picture
    106 on at
    RE: Adding options to my Option Set.

    Hi Leo, i encountered an issue right now.

    So the onChange is working as required, now when i do not select any option and save there is no problem.

    But, when i select an option and then try to save i get an error as follows :

    One or more of the option values for this picklist are not in the range of allowed values.

    Error code: 0x8004431a

  • KashyapT Profile Picture
    106 on at
    RE: Adding options to my Option Set.

    No change(Objects aren't added to the Option Set { ttoptionset } ), i would like to stress on "ttoptionset" being an optionSet field with no values.

    Had to refresh the page, working perfect.
    Thanks!

  • Verified answer
    LeoAlt Profile Picture
    16,331 Moderator on at
    RE: Adding options to my Option Set.

    Hi partner,

    I've updated your code, please have a try.

    function test(executionContext) {
        var formcontext=executionContext.getFormContext();
        var field = formcontext.getControl('new_ttoptionset');
        var lookupObj = formcontext.getAttribute('new_divisionlookup');
        var lookupObjValue = lookupObj.getValue();//Check for Lookup Value
        if (lookupObjValue != null) {
            var lookupRecordGuid = lookupObjValue[0].id;
            Xrm.WebApi.online.retrieveRecord("account"lookupRecordGuid"?$select=new_classname2,new_classname3,new_classname4").then(
                function success(result) {
                    var new_classname2 = result["new_classname2"];
                    var new_classname3 = result["new_classname3"];
                    var new_classname4 = result["new_classname4"];
                    formcontext.getAttribute('new_cname1').setValue(new_classname2);
                    formcontexte.getAttribute('new_cname2').setValue(new_classname3);
                    formcontext.getAttribute('new_cname3').setValue(new_classname4);
                    
                        if (field == null || field != null) { 
                            var new_classname222 = formcontext.getAttribute('new_cname1').getValue();
                            var new_classname223 = formcontext.getAttribute('new_cname2').getValue();
                            var new_classname224 = formcontext.getAttribute('new_cname3').getValue();

                            var obj1 = {};
                            obj1['text'= new_classname222;
                            obj1['value'= 10;
                            field.addOption(obj1);//add to option set field

                            var obj2 = {};
                            obj2['text'= new_classname223;
                            obj2['value'= 11;
                            field.addOption(obj2);//add to option set field

                            var obj3 = {};
                            obj3['text'= new_classname224;
                            obj3['value'= 12;
                            field.addOption(obj3);//add to option set field
                        }
                    },
                
                function (error) {
                    Xrm.Utility.alertDialog(error.message);
                }
            );
        }
        else
            var lookupObjValue111 = formcontext.getAttribute('new_cname1').setValue(null);
            var lookupObjValue122 = formcontext.getAttribute('new_cname2').setValue(null);
            var lookupObjValue133 = formcontext.getAttribute('new_cname3').setValue(null);
    }

    Best Regards,

    Leo

  • KashyapT Profile Picture
    106 on at
    RE: Adding options to my Option Set.

    function test(){

    var lookupObj = Xrm.Page.getAttribute('new_divisionlookup');

    var lookupObjValue = lookupObj.getValue();//Check for Lookup Value

    if ( lookupObjValue != null) {

    var lookupRecordGuid = lookupObjValue[0].id;

    Xrm.WebApi.online.retrieveRecord("account", lookupRecordGuid, "?$select=new_classname2,new_classname3,new_classname4").then(

       function success(result) {

           var new_classname2 = result["new_classname2"];

           var new_classname3 = result["new_classname3"];

           var new_classname4 = result["new_classname4"];

           var setC2 = Xrm.Page.getAttribute('new_cname1').setValue(new_classname2);

           var setC3 = Xrm.Page.getAttribute('new_cname2').setValue(new_classname3);

           var setC4 = Xrm.Page.getAttribute('new_cname3').setValue(new_classname4);

          var addNewOption = function (executionContext) {

          var field = executionContext.getFormContext().getControl('new_ttoptionset');

          if (field == null || field != null )

          {

             var new_classname222 = Xrm.Page.getAttribute('new_cname1').getValue();

             var new_classname223 = Xrm.Page.getAttribute('new_cname2').getValue();

             var new_classname224 = Xrm.Page.getAttribute('new_cname3').getValue();

             var obj={};

             obj['text']= new_classname222  ;

             obj['value']=10;

             field.addOption(obj);//add to option set field

          }

    }    

        },

       function(error) {

           Xrm.Utility.alertDialog(error.message);

       }

    );

    }

    else

    var lookupObjValue111 = Xrm.Page.getAttribute('new_cname1').setValue(null);

    var lookupObjValue122 = Xrm.Page.getAttribute('new_cname2').setValue(null);

    var lookupObjValue133 = Xrm.Page.getAttribute('new_cname3').setValue(null);

    }

    new_ttoptionset is the field where i want to add values from new_classname2,3 and 4, but the above code is not successful of doing so.

  • LeoAlt Profile Picture
    16,331 Moderator on at
    RE: Adding options to my Option Set.

    Hi Kashyap,

    Well, you could use the following code to add values as new options to option set field.

    var addNewOption = function (executionContext) {
        //get the option set field
        var field = executionContext.getFormContext().getControl("new_opt1");
        if (field != null) {

            var obj={};
            obj["text"]="customOption";//new option's label
            obj["value"]=10;//new option's value
            field.addOption(obj);//add to option set field
        }
    }

    Please replace the option set filed name, new options' label and value with yours.

    Best Regards,

    Leo

  • KashyapT Profile Picture
    106 on at
    RE: Adding options to my Option Set.

    I want to add the value as options to the option set.

  • LeoAlt Profile Picture
    16,331 Moderator on at
    RE: Adding options to my Option Set.

    Hi partner,

    So what's the "add the value to an option set" mean? Set the value to option set or add the value as new options to option set? You could refer to my last answer above.

    Best Regards,

    Leo

  • KashyapT Profile Picture
    106 on at
    RE: Adding options to my Option Set.

    I want to add the values which are mapped to entity Y to an option set.

  • LeoAlt Profile Picture
    16,331 Moderator on at
    RE: Adding options to my Option Set.

    Hi Kashyap,

    I've updated my follow up.

    Best Regards,

    Leo

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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 51 Most Valuable Professional

#2
Ramesh Kumar Profile Picture

Ramesh Kumar 42

#3
David Shaw_UK Profile Picture

David Shaw_UK 27

Featured topics

Product updates

Dynamics 365 release plans