Update field with value of correct data type in CRM Plugin
Update Lookup field from CRM Plugin
This will be extension/replacement for the same to make it dynamics to work with any data type.
In this post, we will use Entity metadata to determine the Data type of the field:
"serviceObject" : Service Object
"entityName" : Entity Schema Name
"fieldName" : Field Schema Name
"newValue" : New value for the field
public object getDataTypeSpecificValue(IOrganizationService serviceObject, string entityName, string fieldName, string newValue)
{
object returnValue = "";
//Entity Request
RetrieveEntityRequest entityRequest = new RetrieveEntityRequest();
entityRequest.EntityFilters = EntityFilters.All;
entityRequest.LogicalName = entityName;
//Entity Response
RetrieveEntityResponse entityResponse = (RetrieveEntityResponse)serviceObject.Execute(entityRequest);
//Entity Metadata
EntityMetadata entityMetadata = entityResponse.EntityMetadata;
foreach (var item in entityMetadata.Attributes)
{
if (item.LogicalName.ToString().Equals(fieldName))
{
switch (item.AttributeType.Value.ToString())
{
case "Money":
returnValue = new Money(Convert.ToDecimal(newValue));
break;
case "Decimal":
returnValue = Convert.ToDecimal(newValue);
break;
case "Boolean":
returnValue = Convert.ToBoolean(newValue);
break;
case "Integer":
returnValue = Convert.ToInt32(newValue);
break;
case "Picklist":
returnValue = new OptionSetValue(Convert.ToInt32(newValue));
break;
default:
break;
}
}
}
return returnValue;
}
You can add more datatype cases as needed. this is just an example of how you can do this and then you can manage to use this for your specific case.
Questions/comments/suggestions? please put it in comments below post for further discussion!!!
No comments:
Post a Comment