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:

  1. "Value": This column stores the string value assigned to each picklist option.
  2. "AttributeName": This column contains the logical name of the attribute associated with the picklist values.
  3. "AttributeValue": This column stores the numeric value assigned to each picklist option. Numeric value assigned to picklist option is unique.
  4. "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

Popular posts from this blog

Accessing Fields on QuickView Form through javaScript

ADF - (Part 2) Integrate Data From CRM to External system

ADF - (Part3) Integrate Data From CRM to External system Using Stored procedure