Thursday, September 14, 2017

Update field with value of correct data type in CRM Plugin

I have recently came across a situation where need to update a field with correct datatype in my CRM Plugin.

Example:
Entity entity = new Entity();
entity = service.Retrieve("new_entityname", new Guid(guid), new ColumnSet(true));
This line could be one of options later in this post:
service.Update(entity);

Where,
guid = guid of the Entity
new_fieldName = schema name of field
new_entityname = schema name of Entity
newValue = string value of the field

The 3rd line in above example could be one of below depending on data type, or you can add a switch statement to work with all.

Money:
entity["new_fieldName"] = new Money(Convert.ToDecimal(newValue));

Decimal:
entity["new_fieldName"] = Convert.ToDecimal(newValue);

Boolean:
entity["new_fieldName"] = Convert.ToBoolean(newValue);

String:
entity["new_fieldName"] = newValue;

Integer:
entity["new_fieldName"] = Convert.ToInt32(newValue);

Picklist:
entity["new_fieldName"] = new OptionSetValue(Convert.ToInt32(newValue));

Questions/comments/suggestions? please put it in comments below post for further discussion!!!

Follow By Email for more updates directly into your inbox...


No comments:

Post a Comment