Hi All, Using OData service we are not able to get the option set selected text of an entity. Its providing only value field, but not the text. so I have used Metadata service to retrieve Option set text for the selected value.
Here I am retrieving State option set text from country entity and assigning that option text to text field.
function RetrieveOptionsetLabel()
{
// Entity schema name
var entityLogicalName = "new_country";
// option set schema name
var RetrieveAttributeName = "new_state";
// Target Field schema name to which optionset text needs to be assigned
var AssignAttributeName = "new_state";
// Option set value for which label needs to be retrieved
var stateValue = optionValue;
// Calling Metadata service to get Optionset Label
SDK.MetaData.RetrieveEntityAsync(SDK.MetaData.EntityFilters.Attributes, entityLogicalName, null, false, function (entityMetadata) { successRetrieveEntity(entityLogicalName, entityMetadata, RetrieveAttributeName, stateValue, AssignAttributeName); }, errorDisplay);
}
// Called upon successful metadata retrieval of the entity
function successRetrieveEntity(logicalName, entityMetadata, RetrieveAttributeName, OptionValue, AssignAttributeName) {
///<summary>
/// Retrieves attributes for the entity
///</summary>
var success = false;
for (var i = 0; i < entityMetadata.Attributes.length; i++) {
var AttributeMetadata = entityMetadata.Attributes[i];
if (success) break;
if (AttributeMetadata.SchemaName.toLowerCase() == RetrieveAttributeName.toLowerCase()) {
for (var o = 0; o < AttributeMetadata.OptionSet.Options.length; o++) {
var option = AttributeMetadata.OptionSet.Options[o];
if (option.OptionMetadata.Value == OptionValue) {
Xrm.Page.getAttribute(AssignAttributeName).setValue(option.OptionMetadata.Label.UserLocalizedLabel.Label);
success = true;
break;
}
}
}
}
}
function errorDisplay(XmlHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
Note: Dont forget to add "sdk.metadata.js" resource to form before calling these methods. you can find this library in sdk "sdk\samplecode\js\soapforjscript\soapforjscript\scripts".
what security role privilege needs to be enabled in order to execute this call? I have the code working when run as sys admin, but it fails for regular users.
ReplyDeleteHi Andrew,
ReplyDeleteUser needs to have at least Read privileges on Entity, Attributes, Relationships on Customization Tab of the security Role.
make sure you have configured those settings..
Thank you very much but it raised error in case of Status column
ReplyDeleteI added this code
var option = AttributeMetadata.OptionSet.Options[o];
if (option.OptionMetadata!=null&& option.OptionMetadata.Value == OptionValue) {
AssignAttributeName.innerText = option.OptionMetadata.Label.UserLocalizedLabel.Label;
success = true;
break;
}
if (option.StatusOptionMetadata != null && option.StatusOptionMetadata.Value == OptionValue) {
AssignAttributeName.innerText = option.StatusOptionMetadata.Label.UserLocalizedLabel.Label;
success = true;
break;
}
Hi Ahmed,
DeleteFor Status(schema name : statecode) use StateOptionMetadata
For StatusReason (schema name: statuscode) use StatusOptionMetadata
Hi Ahmed,
ReplyDeleteWhat kind of error you are getting?
Guru, is this still the only way to retrieve option set text?
ReplyDeleteThis is one of the way to retrieve option set text. i'm not sure about the other possible options.
DeleteJust starting with 2011, I've added code to accces the SDK.metadata methids, and added the script to a form. However, I get this error:
ReplyDelete"Unable to get value of the property 'RetrieveEntityAsync': object is null or undefined"
It sounds to me lie I'm not loading the resource properly. Any clues?
check 'RetrieveEntityAsync' object available or not
DeleteI tried using the same code but its not calling my sdk.metadata.js library.
ReplyDeleteAnd it is giving an error called "sdk is undefined".
please share your experience on the same if you have also faced it.
Thanks
Hi All,
ReplyDeleteIts pretty simple to get the optionset label from the particular entity and attribute
Just add the SDK.Metadata.js to your CRM form (you can find this under sdk\samplecode\js\soapforjscript\soapforjscript\scripts)
And then Create one more webresource and add the below javascript function to it.
function GetOptionSetLabel(EntityLogicalName, AttributeLogicalName)
{
SDK.Metadata.RetrieveAttribute(EntityLogicalName, AttributeLogicalName, "00000000-0000-0000-0000-000000000000", true,
function (result) {
for (var i = 0; i < result.OptionSet.Options.length; i++) {
var text = result.OptionSet.Options[i].Label.LocalizedLabels[0].Label;
var value = result.OptionSet.Options[i].Value;
}
},
function (error) { }
);
}
And its done.