Wednesday 16 March 2011

ObjectTypeCode - String or Int

I came across a handy little feature today. I have tested this and it works in Crm 4.0 as well as Crm 2011. Funny how I never knew this before. It would not have changed the world, but it would have made the odd task a little easier.

It seems that it is possible to use the object type codes (1,2 etc) interchangeably as integers or strings ("account", "contact") in a QueryExpression (and everywhere else, I guess). Since the Object Type Codes are always stored as integers in the database, I assumed that they will always need to be queried as integers.

For example, if you want all the tasks that has an Account as a Regarding Object you'd need to know what the Object Type Code for Account is. Now anyone that has dabbled in Crm for a while will know that Account = 1 and Contact = 2, but all custom entities start at 10,000 and exporting customizations from one Organization to another does not mean that the custom entity would have the same code. So, in the example above, I would have done something like this, because I knew that the code of 1 will always represent an Account:

QueryExpression query = new QueryExpression()
{
EntityName = "task",
ColumnSet = new AllColumns(),
Criteria =
{
Conditions =
{
new ConditionExpression()
{
AttributeName = "regardingobjecttypecode",
Operator = ConditionOperator.Equal,
Values = new object[] {1}
}
}
}
};

But for custom entities I would have had to find the current code for the entity first and pass that through to the QueryExpression.

Turns out that you can actually pass the string equivalent as a value as well !!!

QueryExpression query = new QueryExpression()
{
EntityName = "task",
ColumnSet = new AllColumns(),
Criteria =
{
Conditions =
{
new ConditionExpression()
{
AttributeName = "regardingobjecttypecode",
Operator = ConditionOperator.Equal,
Values = new object[] {"account"}
}
}
}
};

Brilliant!!

Paul Reyneke

No comments:

Post a Comment