Dynamically add options to an Option Set using JavaScript

Dynamically add options to an Option Set using JavaScript

Many times you  have requirement like adding options in an existing option set without updating database values.you can achieve this by using following function:-

function AddOptions(){
//Use this line of code if you do not want to display existing values
var optionset=Xrm.Page.getControl("optionset").clearOptions();
//create option Array to be added
var options=new Object();
options=[{value:1,text:"testoption1"},{value:12,text:"testoption2"},{value:13,text:"testoption3"}];
//set this value in optionset
optionset.addOption(options[0],1);
optionset.addOption(options[1],2);
optionset.addOption(options[2],3);

}

Comments

  1. try for this
    var preferredTimeOptionSet = Xrm.Page.ui.controls.get("new_shiptoaddress2");
    var optionsSets = preferredTimeOptionSet.getAttribute().getOptions();

    preferredTimeOptionSet.clearOptions();
    optionsSets.push({ "value": "2", "text": "anup" });
    preferredTimeOptionSet .addOption(optionsSets[0],1);
    preferredTimeOptionSet .addOption(optionsSets[1],2);
    preferredTimeOptionSet .addOption(optionsSets[3],3);

    ReplyDelete
  2. I am trying to set the firstnames to optionset using the below :

    GetFirstname = Xrm.Page.getControl("new_firstname");
    GetFirstname.addOption({ text: value.firstname, value: key });

    The above is adding all the desired firstnames to the optionset. But, when I am selecting any firstname , it is not displayed .

    Any help ?

    ReplyDelete
  3. Same problem with me also. Values successfuly added to optionset but not able to select them.

    Please advise

    ReplyDelete

Post a Comment

Popular Posts