I've found code which nearly does what I want to do (from http://guruprasadcrm.blogspot.co.uk/2011/07/retrieving-optionset-lable-data-using.html)
I can't get the value of the OptionSet text in the code though.
This is the modified code I have:
function GetOptionSetLabel(EntityLogicalName, AttributeLogicalName, OptionSetValue)
{
var optionSetText = "";
SDK.Metadata.RetrieveAttribute(EntityLogicalName, AttributeLogicalName, "00000000-0000-0000-0000-000000000000", false,
function (result) {
for (var i = 0; i < result.OptionSet.Options.length; i++) {
var loopText = result.OptionSet.Options[i].Label.LocalizedLabels[0].Label;
var loopValue = result.OptionSet.Options[i].Value;
-- I want to do a check here but this is in a different function
}
},
function (error) { }
);
return optionSetText;
}
Any ideas?
*This post is locked for comments
Hi,friend!
You can try the following method:
For Example:
Xrm.Page.getAttribute("statuscode").getSelectedOption().text
Xrm.Page.getAttribute("statuscode").getSelectedOption().value
Here is a generic way to get optionset values in C#, Rest API Javascript and CRM Formscript
vjcity.blogspot.com/.../generic-way-to-get-optionset-value-or.html
So I finally worked out how to get this working in CRM 2015.
The working function is similar to the one I posted in my question:
function GetOptionSetLabel(EntityLogicalName, AttributeLogicalName, OptionSetValue)
{
var optionSetText = "";
SDK.Metadata.RetrieveAttribute(EntityLogicalName, AttributeLogicalName, "00000000-0000-0000-0000-000000000000", false,
function (result) {
for (var i = 0; i < result.OptionSet.Options.length; i++) {
var loopText = result.OptionSet.Options[i].Label.LocalizedLabels[0].Label;
var loopValue = result.OptionSet.Options[i].Value;
if (OptionSetValue == loopValue) {
optionSetText = loopText;
}
}
},
function (error) { }
);
return optionSetText;
}
I also had to edit a line in SDK.MetaData.js to make the SDK.MetaData.RetrieveAttribute synchronous. I wasn't entirely happy about this but it did work. (found in a comment by Veer Balla in https://community.dynamics.com/crm/f/117/t/167882)
change
req.open("POST", _getUrl() + "/XRMServices/2011/Organization.svc/web", true);
to
req.open("POST", _getUrl() + "/XRMServices/2011/Organization.svc/web", false);
You can use webapi to retrieve the values and labels from particular optionsset, which will return the data in JSON format.
The following webapi call will return the accountcategorycode values in json format for the account entity, You can do the manipulation after that:
orgname.contoso.com/.../EntityDefinitions(LogicalName='account')/Attributes(LogicalName='accountcategorycode')/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet($select=Options),GlobalOptionSet($select=Options)
This will return the following:
{ "@odata.context":"orgname.contoso.com/.../v8.2$metadata#EntityDefinitions('account')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata(LogicalName,OptionSet,GlobalOptionSet,OptionSet(Options),GlobalOptionSet(Options))/$entity","LogicalName":"accountcategorycode","MetadataId":"118771ca-6fb9-4f60-8fd4-99b6124b63ad","OptionSet@odata.context":"orgname.contoso.com/.../v8.2$metadata#EntityDefinitions('account'#/Attributes#118771ca-6fb9-4f60-8fd4-99b6124b63ad)/Microsoft.Dynamics.CRM.PicklistAttributeMetadata/OptionSet(Options)/$entity","OptionSet":{ "Options":[ { "Value":1,"Label":{ "LocalizedLabels":[ { "Label":"Preferred Customer","LanguageCode":1033,"IsManaged":true,"MetadataId":"0bd8a218-2341-db11-898a-0007e9e17ebd","HasChanged":null } ],"UserLocalizedLabel":{ "Label":"Preferred Customer","LanguageCode":1033,"IsManaged":true,"MetadataId":"0bd8a218-2341-db11-898a-0007e9e17ebd","HasChanged":null } },"Description":{ "LocalizedLabels":[ ],"UserLocalizedLabel":null },"Color":null,"IsManaged":true,"MetadataId":null,"HasChanged":null },{ "Value":2,"Label":{ "LocalizedLabels":[ { "Label":"Standard","LanguageCode":1033,"IsManaged":true,"MetadataId":"0dd8a218-2341-db11-898a-0007e9e17ebd","HasChanged":null } ],"UserLocalizedLabel":{ "Label":"Standard","LanguageCode":1033,"IsManaged":true,"MetadataId":"0dd8a218-2341-db11-898a-0007e9e17ebd","HasChanged":null } },"Description":{ "LocalizedLabels":[ ],"UserLocalizedLabel":null },"Color":null,"IsManaged":true,"MetadataId":null,"HasChanged":null } ],"MetadataId":"b994cdd8-5ce9-4ab9-bdd3-8888ebdb0407" },"GlobalOptionSet":null }
Hope this helps.
Use below code to get OptionSet Value
Xrm.Page.getAttribute("fieldname").getValue()
this for C# not java script
to get option set value use
int r = ((OptionSetValue)e["f"]).Value; // f is name of the option set the filed
and to get the text for this value use the other code
use this
int r = ((OptionSetValue)e["f"]).Value; // f is name of the option set the filed
to get text of this data we write :
RetrieveAttributeRequest a = new RetrieveAttributeRequest()
{
EntityLogicalName = e.LogicalName,
LogicalName = "f",
RetrieveAsIfPublished = true
};
RetrieveAttributeResponse y = (RetrieveAttributeResponse)service.Execute(a);
PicklistAttributeMetadata l = (PicklistAttributeMetadata)y.AttributeMetadata;
OptionMetadata[] o = l.OptionSet.Options.ToArray();
foreach (OptionMetadata m in o) {
if(m.Value==r){ // r is data value
string r2 = m.Label.UserLocalizedLabel.Label;
}}
I have seen this page but the function doesn't actually do anything with the text and value of the optionset. I want to return the text if it matches a value
You can refer this:
meghshyam.wordpress.com/.../retrieving-the-optionset-label-using-javascript-in-crm-2011crm-2013
Use
SDK.Metadata.RetrieveAttribute(EntityLogicalName, AttributeLogicalName, "00000000-0000-0000-0000-000000000000", false,
function (result)
Instead of
SDK.Metadata.RetrieveAttribute(EntityLogicalName, AttributeLogicalName, “00000000-0000-0000-0000-000000000000”, true,
function (result)
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156