Get option-set value from text and get option-set text from Value using plugin
In this blog we will discuss the use of StringMap table to get option-set metadata in our CRM plugin.
StringMap table holds the metadata information of option-sets.
The StringMap table contains the following important columns:
- "Value": This column stores the string value assigned to each picklist option.
- "AttributeName": This column contains the logical name of the attribute associated with the picklist values.
- "AttributeValue": This column stores the numeric value assigned to each picklist option. Numeric value assigned to picklist option is unique.
- "ObjectTypeCode": Object type code of entity for which attribute belongs.
Code snippet to get option-set Value from Text:
static int? GetOptionSetValueFromText(IOrganizationService service, string optionSetSchemaName, string optionSetText, int objectTypeCode)
{
string fetch = @"<fetch>
<entity name='stringmap' >
<attribute name='value' />
<attribute name='objecttypecode' />
<attribute name='attributevalue' />
<filter type='and' >
<condition attribute='objecttypecode' operator='eq' value='" + objectTypeCode + @"' />
<condition attribute='attributename' operator='eq' value='"+optionSetSchemaName+ @"' />
<condition attribute='value' operator='eq' value='"+optionSetText+@"' />
</filter>
</entity>
</fetch>";
EntityCollection stringMapCollection = service.RetrieveMultiple(new FetchExpression(fetch));
int? optionSetValue = null;
if (stringMapCollection.Entities.Count > 0)
{
optionSetValue = stringMapCollection.Entities[0].GetAttributeValue<int>("attributevalue");
}
return optionSetValue;
}
Code snippet to get option-set Text from Value:we can use simple formatted Values syntax to get the option-set Text
string type = account.FormattedValues["new_assignmenttype"];
Code snippet to get option-set Text from Value using stringmap table:
static string GetOptionSetTextFromValue(IOrganizationService service, string optionSetSchemaName, int optionsetValue, int objectTypeCode)
{
string fetch = @"<fetch>
<entity name='stringmap' >
<attribute name='value' />
<attribute name='objecttypecode' />
<attribute name='attributevalue' />
<filter type='and' >
<condition attribute='objecttypecode' operator='eq' value='" + objectTypeCode + @"' />
<condition attribute='attributename' operator='eq' value='" + optionSetSchemaName + @"' />
<condition attribute='attributevalue' operator='eq' value='" + optionsetValue + @"' />
</filter>
</entity>
</fetch>";
EntityCollection stringMapCollection = service.RetrieveMultiple(new FetchExpression(fetch));
string optionSetText = null;
if (stringMapCollection.Entities.Count > 0)
{
optionSetText = stringMapCollection.Entities[0].GetAttributeValue<string>("value");
}
return optionSetText;
}
Comments
Post a Comment