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 :
Microsoft Dynamics CRM (Archived)

Show / hide options in optionset value

(0) ShareShare
ReportReport
Posted on by 1,000

How can i dynamically show/hide the options from option set  by using javascript. I use the following code for remove option 

Xrm.Page.getControl("new_fieldname").removeOption(802790001)

It was working fine .But i need to show the option dynamically using script. I used addOption . but its was not working

How can i show the optionset options dynamically?

Thanks in Advance

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Biplab Singha Profile Picture
    on at

    Hi Vijay,

    For Using addOption those options Need to be there in Metadata first. You can't add a option that is not present in Metadata. Example can be Suppose you have Option 1, 2 and 3 in Metadata. now you want to add another option 4 using Javascript "addOption" . here it is not possible.

    In you Your Case I will suggest get a maximum possible set or Options in the optionset now onload of form or Onchange of field "removeOption " the options that are not required.

    Mark the answer as verified if it helps

  • Community Member Profile Picture
    on at

    Hi

    please copy code  that i have posted in this link.... it will filter out optionset as per your req

    community.dynamics.com/.../511381

    Please mark verified if it suits your query!!!1

    Regards,

    Nithin

  • vijay12872 Profile Picture
    1,000 on at

    My optionset already having 1,2,3 values in it. based on the another field (field name like X) selection  i need to hide 3. By using removeoption it was working fine. Again am going to change X field value i need show 3 again in the optionset. This is my problem. I didnt try to add new option.Already the options is there just i need to show/hide based on the field X selection

  • Community Member Profile Picture
    on at

    hi vijay,

    Please modify the code , i am sure it will work fine

    function optionSetChanged() {

      ///<summary>

      /// Change the dependent picklist values based on the value selected in the control picklist.

      ///</summary>

    var _selectedCasetype = Xrm.Page.getAttribute("new_casetype").getText();

    if(_selectedCasetype != null){

      var _collection = getCollection();

      var _selectedCity = null;

      var _subCaseCat = Xrm.Page.ui.controls.get("new_subcasetype");

      if (_subCaseCat != null)

          _selectedCity = _subCaseCat.getAttribute().getValue();

      var _subCaseOptions = _subCaseCat.getAttribute().getOptions();

      // If Main_Case_Type is empty, then clear the CitIES field.

      if (_selectedCasetype == "") {

          _subCaseCat.clearOptions();

      }

      else {

          for (var i = 0; i < _collection.length; i++) {

              if (_selectedCasetype.toLowerCase() == _collection[i].Main_Case_Type.toLowerCase()) {

                  _subCaseCat.clearOptions();

                  for (var j = 0; j < _collection[i].Sub_Case_Type.length; j++) {

                      for (var k = 0; k < _subCaseOptions.length; k++) {

                          if (_collection[i].Sub_Case_Type[j].toLowerCase() == _subCaseOptions[k].text.toLowerCase()) {

                              _subCaseCat.addOption(_subCaseOptions[k]);

                              break;

                          }

                      }

                  }

                  break;

              }

          }

          if (_subCaseCat != null && _selectedCity != null)

              _subCaseCat.getAttribute().setValue(_selectedCity);

      }

    }

    else{

    alert("Please select an option");

    Xrm.Page.getAttribute("new_subcasetype").setValue(null);

    }

    }

    function getCollection() {

      ///<summary>

      /// Creates and returns a collection of Sub_Case_Type with respect to their Countries.

      ///</summary>

      var _collection = new Array();

      var CARDS_SubCat = new Array("CARD ACTIVATION", "CARD LOST", "CHANGE OF FIN", "CHANGE OF PHOTO", "FAULTY", "LOST & FOUND", "NON REC OF CARDS");

      var CARDS_obj = { Main_Case_Type: "CARDS", Sub_Case_Type: CARDS_SubCat };

      _collection.push(CARDS_obj);

      var LINKPOINTS_SubCat = new Array("CHECK LPS BAL", "EXP LPS", "OCBC STMT LPS", "LPs Discrepancy", "Duplicate LPs Crediting", "Duplicate LPs Reversal", "LPs Adjustment", "LPs Reversal", "Multiply redemption", "");

      var LINKPOINTS_obj = { Main_Case_Type: "LINKPOINTS", Sub_Case_Type: LINKPOINTS_SubCat };

      _collection.push(LINKPOINTS_obj);

      var ONLINE_REDEMPTION_SubCat = new Array("PROMOTION", "STARBUY", "KC Campaign", "KE Campaign", "DS Campaign", "Crazy LPs", "LBTC", "Reward Items");

      var ONLINE_REDEMPTION_obj = { Main_Case_Type: "ONLINE REDEMPTION", Sub_Case_Type: ONLINE_REDEMPTION_SubCat };

      _collection.push(ONLINE_REDEMPTION_obj);

    var UPDATE_PARTICULARS_SubCat = new Array("Update Name", "Update Email Address", "Update Mobile No", "Update Residential No", "Update IC/FIN", "Crazy LPs", "Update Address");

      var UPDATE_PARTICULARS_obj = { Main_Case_Type: "UPDATE PARTICULARS", Sub_Case_Type: UPDATE_PARTICULARS_SubCat };

      _collection.push(UPDATE_PARTICULARS_obj);

    var Service_Recovery_SubCat = new Array("Lapse of Service", "Technical Glitch", "Vendor Error", "MED Request", "Deceased Member", "Resigned Member");

      var Service_Recovery_obj = { Main_Case_Type: "Service Recovery", Sub_Case_Type: Service_Recovery_SubCat };

      _collection.push(Service_Recovery_obj);

    var OTHERS_SubCat = new Array("COMPLAINT", "FOODFARE STOREVALUE", "FP REBATES", "OCBC VISA QUERIESt", "RECOGNITION EVENT", "ONLINE SURVEY");

      var OTHERS_obj = { Main_Case_Type: "OTHERS", Sub_Case_Type: OTHERS_SubCat };

      _collection.push(OTHERS_obj);

    var Outbound_Calls_SubCat = new Array("Events", "Promotion");

      var Outbound_Calls_obj = { Main_Case_Type: "Outbound Calls", Sub_Case_Type: Outbound_Calls_SubCat };

      _collection.push(Outbound_Calls_obj);

    var PDPA_SubCat = new Array("Do Not Call", "Do Not SMS", "Do Not Mail", "Do Not Email", "Do Not Fax", "Do Not Send Card", "Unsub All Channels");

      var PDPA_obj = { Main_Case_Type: "PDPA", Sub_Case_Type: PDPA_SubCat };

      _collection.push(PDPA_obj);

    var PDPA_Consent_SubCat = new Array("Call", "SMS", "Mail", "Email", "Fax", "All Channels");

      var PDPA_Consent_obj = { Main_Case_Type: "PDPA Consent", Sub_Case_Type: PDPA_Consent_SubCat };

      _collection.push(PDPA_Consent_obj);

      return _collection;

    }

    Hope it helps !! If so, Mark my answer as Verified !!

    Regards,

    Nithin

  • Verified answer
    Zohaib Uddin Khan Profile Picture
    2,822 on at

    Hi,

    Read Info at OptionSet Control Methods.

    Xrm.Page.getControl(arg).addOption(option, [index])

    System_CAPS_importantImportant

    This method doesn’t check that the values in the options you add are valid. If you add invalid options they will not work correctly. You must only add options that have been defined for the specific option set attribute that the control is bound to. Use the attribute getOptions orgetOption methods to get valid option objects to add using this method.

    Arguments
    
    option --> Type: Object: An option object to add to the OptionSet.
    index  --> Type: Number: (Optional) The index position to place the new option in. 
    If not provided, the option will be added to the end.
    
    
    Example Code:
    
    var Food1 = {value : 100000000, text : "Biryani"};
    var Food2 = {value : 100000001, text : "Korma"};
    
    var pickList = Xrm.Page.getControl("new_fieldname);
    pickList.addOption(Food1);
    pickList.addOption(Food2);
    

  • Suggested answer
    Community Member Profile Picture
    on at
  • Saddamk206 Profile Picture
    777 on at

    Hi,

    Also can try this:-

    for (var i = 0; i < Xrm.Page.getAttribute("FieldName").getOptions().length; i++)
    {

    Xrm.Page.getControl("FieldName").removeOption(Xrm.Page.getAttribute("FieldName").getOptions()[i].value);
    }

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans