Home > CRM 2011, JScript > Reading option set using JScript in CRM 2011

Reading option set using JScript in CRM 2011

Hi,

Below is the Jscript functions to read “Option set” and get the option text by option value.

In this approach I have 3 methods and details are below

  • getOptionSet() – Returns optionset object
    • Parameters
      • Entity Name           (Entity logical name)
      • Attribute Name        (i.e., Option set field name)
      • retrieveAsIfPublished (True/False)
  • getTextByValue() – Returns option set text by passing value
    • Parameters
      • optionSet        (i.e., Option set object got from above method)
      • attributeValue   (Option set field value)
  • getSOAPWrapper – Helper method

function getOptionSet (entityLogicalName, attributeLogicalName, retrieveAsIfPublished) {

var serverUrl = Xrm.Page.context.getServerUrl();

var ODataPath = serverUrl + “/XRMServices/2011/Organization.svc/web”;

var MetadataId = “00000000-0000-0000-0000-000000000000”;

var request = “<Execute xmlns=\”http://schemas.microsoft.com/xrm/2011/Contracts/Services\” xmlns:i=\”http://www.w3.org/2001/XMLSchema-instance\“>”;

request += “<request i:type=\”a:RetrieveAttributeRequest\” xmlns:a=\”http://schemas.microsoft.com/xrm/2011/Contracts\“>”;

request += “<a:Parameters xmlns:b=\”http://schemas.datacontract.org/2004/07/System.Collections.Generic\“>”;

request += “<a:KeyValuePairOfstringanyType>”;

request += “<b:key>EntityLogicalName</b:key>”;

request += “<b:value i:type=\”c:string\” xmlns:c=\”http://www.w3.org/2001/XMLSchema\“>” + entityLogicalName + “</b:value>”;

request += “</a:KeyValuePairOfstringanyType>”;

request += “<a:KeyValuePairOfstringanyType>”;

request += “<b:key>MetadataId</b:key>”;

request += “<b:value i:type=\”ser:guid\”  xmlns:ser=\”http://schemas.microsoft.com/2003/10/Serialization/\“>” + MetadataId + “</b:value>”;

request += “</a:KeyValuePairOfstringanyType>”;

request += “<a:KeyValuePairOfstringanyType>”;

request += “<b:key>RetrieveAsIfPublished</b:key>”;

request += “<b:value i:type=\”c:boolean\” xmlns:c=\”http://www.w3.org/2001/XMLSchema\“>” + retrieveAsIfPublished + “</b:value>”;

request += “</a:KeyValuePairOfstringanyType>”;

request += “<a:KeyValuePairOfstringanyType>”;

request += “<b:key>LogicalName</b:key>”;

request += “<b:value i:type=\”c:string\”   xmlns:c=\”http://www.w3.org/2001/XMLSchema\“>” + attributeLogicalName + “</b:value>”;

request += “</a:KeyValuePairOfstringanyType>”;

request += “</a:Parameters>”;

request += “<a:RequestId i:nil=\”true\” /><a:RequestName>RetrieveAttribute</a:RequestName></request>”;

request += “</Execute>”;

request = _getSOAPWrapper(request);

var req = new XMLHttpRequest();

req.open(“POST”, ODataPath, false);

req.setRequestHeader(“Accept”, “application/xml, text/xml, */*”);

req.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);

req.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute“);

req.send(request);

if (req.responseXML != null) {

var attributeData = req.responseXML.selectSingleNode(“//b:value”);

if (attributeData != null) {

var attributeType = attributeData.selectSingleNode(“c:AttributeType”).text;

switch (attributeType) {

case “Picklist”:

return attributeData;

break;

default:

break;

}

}

}

}

function _getSOAPWrapper(request) {

var SOAP = “<soapenv:Envelope xmlns:soapenv=\”http://schemas.xmlsoap.org/soap/envelope/\“><soapenv:Body>”;

SOAP += request;

SOAP += “</soapenv:Body></soapenv:Envelope>”;

return SOAP;

}

function getTextByValue(optionSet, attributeValue) {

var options = optionSet.selectSingleNode(“c:OptionSet//c:Options”);

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

var value = options.childNodes[i].selectSingleNode(“c:Value”).text;

if (value == attributeValue) {

var text = options.childNodes[i].selectSingleNode(“c:Label”).selectSingleNode(“a:UserLocalizedLabel”).selectSingleNode(“a:Label”).text;

return text;

}

}

}

How do I call this method

  • Copy the above functions and create a Jscript webresource in CRM
  • In below example I am reading “Address Type” option set of entity “Contact”

// Get option set object by passing params

// Entity name, Optionset field name, true/false

var optionSet = getOptionSet(“contact”, “address1_addresstypecode”, true);

if (optionSet) {

// Read option text of value ‘2’

var optionValue = 2;

var optionText = getTextByValue(optionSet, optionValue);

alert(“Text of option value – ” + optionValue + ” is -” + optionText);

// Read option text of value ‘10001’

optionValue = 10001;

optionText = getTextByValue(optionSet, optionValue);

alert(“Text of option value – ” + optionValue + ” is -” + optionText);

}

else {

alert(“Invalid Option set”);

}

Note :-  *** If you copy the code please replace double quotes(“) symbols with your keyboard double quotes ***

Hope it helps 🙂

Advertisement
  1. Harsha
    March 8, 2012 at 11:35 AM

    Exactly seraching for this kind of sample..

  2. Aharon
    March 18, 2012 at 3:44 PM

    thank you very much!

  3. Michael
    August 25, 2012 at 6:44 AM

    Hi, I have problem testing this example. The “attributeData” variable returns null. Is in the “metadataID” variable should put some GUID or there is a problem with the http://schemas.microsoft.com/xrm/2011/Contracts/Services services?

    Thanks a lot

    • August 29, 2012 at 2:29 PM

      Hi Michael,
      Please check “req.responseXML” for the problem
      MetaDataId we pass as Blank GUID

    • Dave
      March 26, 2013 at 10:46 PM

      I just tried this code and getting the same issue with the attributeData variable null. Michael – were you able to figure out how to fix this?

  4. Murali
    April 4, 2013 at 7:32 PM

    attributeData I am getting null What might be the reason can you please tell us

  5. mooowoo
    July 8, 2013 at 1:36 PM

    it works fine on ie9, but it doesn’t work on ie 10 after rollup12.
    the error is something like this :Reference to undeclared namespace prefix: ‘b’

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: