Pages

Wednesday, April 1, 2009

Using activityparty in a partylist for a DynamicEntity

[Full Disclosure: The inspiration for the following code comes from YuvaKumar's post on the matter.]

So, I added custom attributes to my Service Activity entity in CRM 4.0 recently, and then found myself tasked with establishing a Workflow that used them. Since I have a very strong habit of writing my Plug-ins and Workflows for CRM using DynamicEntity for any reference to my custom fields--hell, it's practically the only reference I use--I was a little confused about how to approach the partylist attributes.

That's when I found the post linked above. Now I have a solution to my problem, and I'd like to share how you can use it for yourself in different coding situations:

Long story short, the target record is setup as a Lookup reference of the activityparty entity--established as a DynamicEntity object--which itself is nested in a DynamicEntityArrayProperty of the activity entity.

That's a mouthful. Here's a practical application: Let's say I have the GUID of an Account record in the variable customerId, and I want to establish this record into the customers property of a Service Activity I'm building in a DynamicEntity variable called serviceActivity.

First, I establish a DynamicEntity of the activityparty type:

DynamicEntity activityParty = new DynamicEntity("activityparty"); 

Then, I create a new LookupProperty for the partyid attribute of our new activityparty entity, and set it to our customerId variable:

LookupProperty partyProperty = new LookupProperty("partyid", new Lookup(EntityName.account.ToString(), customerId));
activityParty.Properties.Add(partyProperty);

Finally, I create a new DynamicEntityArrayProperty for the customers attribute of my Service Activity, and load our activityParty into it as a DynamicEntity array:

DynamicEntityArrayProperty customersProperty = new DynamicEntityArrayProperty("customers", new DynamicEntity[] { activityParty }); 
serviceActivity.Properties.Add(customersProperty); 

That's it! But that's just the basics. Obviously, the implications here are that for establishing many activityparty references, you need to load them as an array of DynamicEntity objects into a DynamicEntityArrayProperty for your Activity record. And, these DynamicEntity objects aren't typed with the record you're referencing, but actually typed as an activityparty instance with Lookups to those records.