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 :
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 ?

I have the same question (0)
  • Suggested answer
    Dynamics365 Rocker Profile Picture
    7,755 on at

    You have to write a script onchange of lookup field. In this script retrieve optionset value of entity X and set it in entity Y.

    You can also create workflow or plugin instead of Script.

  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at

    Hi partner,

    Do you mean you want to get value from entity X by Lookup field on entity Y and set the value to option set field on entity Y when saving the form?

    If so, there are two ways for you.

    1.Using quick view form.

    For example, I have an account lookup filed on my custom entity form, and I want to show some accont fields on my custom entity form, so I could add a "quick view form" of account entity on my custom entity form.

    pastedimage1571903738595v2.png

    pastedimage1571903716242v1.png

    Then on my custom entity form  there will be some account fields which related to the account records selected in the lookup filed.

    pastedimage1571904039805v3.png

    You could choose what fields you want to show in quick view form by editing it in Settings->Customizations..

    But note that the fields shown in the quick view form are not really exited in your main form, so you could not save them or edit them.

    https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/customize/create-edit-quick-view-forms

    2. If you want to set the value from field on entity X to feild on entity Y, You could use js code.

    The thinking is get the related entity X data by the lookup field on entity Y form and set the value to field.

    Here is  the sample code for you.

    function getOptionsfromEntityX(executionContext){
        var formContext = executionContext.getFormContext();
        //step1. get the entityX recordid by lookup field.
        var entityXRecord=formContext.getAttribute("entityXLookupfield");
        if(entityXRecord!=null){
            var value=entityXRecord.getValue();
            //get the recordid
            var recordid=value[0].id;
            //get the option set field value on the entityX by web api
            Xrm.WebApi.retrieveRecord("EntityX",recordid,"?$select=optionSetFieldname").then(
                function success(result) {
                    var filedvalue=result.optionSetFieldname;
                    //set the value to field on entity Y
                    formContext.getAttribute.setValue(filedvalue);
                },
                function error(error) {
                    Xrm.Navigation.openAlertDialog({ text: error.message });
                }
            );
        }
    }

    You could add this function to the "Onsave" event of entity Y form.

    pastedimage1571904594500v4.png

    pastedimage1571904612997v5.png

    And don't forget to select "Pass execution context as first parameter".

    Hope it helps.

    Best Regards,

    Leo

  • KashyapT Profile Picture
    106 on at

    So i have been able to get values from Entity X(which is looked up in Entity Y) and set them to fields in Entity Y, all of this onSave of Entity Y.

    All i want now is to add these values to an option set in Entity Y(Also onSave).

  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at

    Hi Kashyap,

    1.If you mean you want to set value to option set field, You could use the following code to set value to option set field.

    // GET and SET OptionSet field value

    var functionName = function (executionContext) {

       // Access the field on the form

       var field = executionContext.getFormContext().getAttribute("customertypecode");

       // Verify it does exist on the form

       if (field != null) {

           // Set its field value

           field.setValue(3);

       }

    }

    2.If you mean you want to add some values as new options to option set field, you could use the following code.

    var addNewOption = function (executionContext) {
        var field = executionContext.getFormContext().getControl("new_opt1");
        if (field != null) {

            var obj={};
            obj["text"]="customOption";
            obj["value"]=10;
            field.addOption(obj);
        }
    }

    pastedimage1571966639031v1.png

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/controls/addoption

    Hope it helps.

    Best Regards,

    Leo

  • LeoAlt Profile Picture
    16,331 Moderator on at

    Hi Kashyap,

    I've updated my follow up.

    Best Regards,

    Leo

  • KashyapT Profile Picture
    106 on at

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

  • LeoAlt Profile Picture
    16,331 Moderator on at

    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

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

  • LeoAlt Profile Picture
    16,331 Moderator on at

    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

    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.

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 > Service | Customer Service, Contact Center, Field Service, Guides

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 49 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 27 Most Valuable Professional

#3
Soundari Profile Picture

Soundari 15

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans