Sunday 22 May 2016

Get and Set option set values/text/object in MS CRM using js

How to get an Option by passing value?
The code below returns an Option object with text and value properties.
Value argument represents the numeric value of the optinonsetvalue field for searching the Option:


 var myOption =Xrm.Page.getAttribute("fieldSchemaname").getOption(value);


How to get selected CRM Option Set Value item?
The code below returns the Option object with text and value properties:


var selectedOption =Xrm.Page.getAttribute("fieldSchemaname").getSelectedOption();

How to get selected value of an CRM Option Set Value?
The code below gives us the numeric value of selected OptionSetValue field:


var optionsetValue = Xrm.Page.getAttribute("fieldSchemaname").getValue();

How to get Text of an CRM Option Set Value?
The code below gives us the text of selected OptionSetValue field:


var optionsetText = Xrm.Page.getAttribute("fieldSchemaname").getText();



How to set an CRM Option Set Value (by numeric value)?
The code below helps us to set the value of OptionSetValue by a numeric value passed to the setValue function:


Xrm.Page.getAttribute("fieldSchemaname").setValue(value);



How to set an CRM Option Set Value by Text?


Actually we can’t set the selected item by text but we can search all options until we find the one whose text (label) is the same as one the one you have then get its value like below:



function setOptionSetValueByText(fieldSchemaname, optionText) {

    var options = Xrm.Page.getAttribute("fieldSchemaname").getOptions();

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

        if (options[i].text == optionText) {

        Xrm.Page.getAttribute(fieldSchemaname).setValue(options[i].value);

        }

    }

}

No comments:

Post a Comment