Oct 30, 2012

Retrieve attributes of entity object

This is basic, but someone who is starting to code plug-ins (and custom codes) in CRM 2011 would be helped.

First visit this, which explains few basic differences of plug-in syntaxes in CRM 2011 with compared to earlier version.

Below are types we can initiate in our codes with their initial values.

Guid myGuid = Guid.Empty; // 00000000-0000-0000-0000-000000000000
String myString = String.Empty; // NULL
EntityReference myEntityReferance = new EntityReference(); //NULL
OptionSetValue myOptionSetValue = new OptionSetValue(); // 0
DateTime myDateTime = new DateTime(); //1/01/0001 12:00:00 AM
Decimal myDecimal = Decimal.Zero; // 0
Money myMoney = new Money(); // 0

Below are some simple ways of reading them from an entity object.

if (TargetEnt.Contains("new_officeid"))
{
myGuid = (Guid)TargetEnt.Attributes["new_officeid"];
}
if (TargetEnt.Contains("new_name"))
{
myString = TargetEnt.Attributes["new_name"].ToString();
}
if (TargetEnt.Contains("new_officestatus"))
{
myOptionSetValue = (OptionSetValue)TargetEnt["new_officestatus"];
}
if (TargetEnt.Contains("createdby"))
{
myEntityReferance = (EntityReference)TargetEnt["createdby"];
}
if (TargetEnt.Contains("createdon"))
{
myDateTime = (DateTime)TargetEnt["createdon"];
}
if (TargetEnt.Contains("new_revenue"))
{
myDecimal = (Decimal)TargetEnt["new_revenue"];
}
if (TargetEnt.Contains("new_cost"))
{
myMoney = (Money)TargetEnt["new_cost"];
}

1 comment:

  1. For bit fields;
    bool _Expired = (bool)TargetEnt.Attributes["new_isexpired"];
    Now
    Value returned are True/False (not 0,1)

    ReplyDelete