Friday, August 29, 2008

Starter Execution Method

I have found that half of my trouble in writing a Plugin has been to figure out what message to use and how to pull the GUID from the record that the Plugin was triggered off of. Most of the messages are reasonably straight forward (Assign, Create, Update, or Delete) but finding what to use when an Opportunity closes took some digging. You'd think it was the SetState message but that's not right (nor is it the SetStateDynamicEntity).

In order to help out with this I have written myself a "Starter Execution Method" that has most of the common messages that I tend to use. I haven't coded against every message as there are now a ton of options but some of the new messages available to us are pretty cool.

Below the starter Execution Method I have also posted the possible MessageName's from the SDK. Hopefully this helps some people started.


Starter Execution Method:

public void Execute(IPluginExecutionContext context)
{
string opportunityid = "";

switch(context.MessageName)
{
case "Create":
if (context.OutputParameters.Properties.Contains("id"))
{
opportunityid = context.OutputParameters.Properties["id"].ToString();
}
break;
case "Update":
if (context.InputParameters.Properties.Contains(ParameterName.Target) && context.InputParameters.Properties[ParameterName.Target] is DynamicEntity)
{
DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];
opportunityid = ((Key)entity.Properties["opportunityid"]).Value.ToString();
}
break;
case "SetState":
if (context.InputParameters.Properties.Contains("EntityMoniker"))
{
if (context.InputParameters.Properties.Contains("State"))
{
entity = (Moniker)context.InputParameters.Properties[ParameterName.EntityMoniker];
opportunityid = entity.Id.ToString();
}
}
break;
case "SetStateDynamicEntity":
if (context.InputParameters.Properties.Contains("EntityMoniker"))
{
if (context.InputParameters.Properties.Contains("State"))
{
entity = (Moniker)context.InputParameters.Properties[ParameterName.EntityMoniker];
opportunityid = entity.Id.ToString();
}
}
break;
case "Win":
opportunityClose = (DynamicEntity)context.InputParameters["OpportunityClose"];
Lookup WonLook = (Lookup)opportunityClose.Properties["opportunityid"];
opportunityid = WonLook.Value.ToString();
break;
case "Lose":
opportunityClose = (DynamicEntity)context.InputParameters["OpportunityClose"];
Lookup LostLook = (Lookup)opportunityClose.Properties["opportunityid"];
opportunityid = LostLook.Value.ToString();
break;
case "Assign":
if (context.InputParameters.Properties.Contains("Assignee") && context.InputParameters.Properties["Assignee"] is SecurityPrincipal)
{
Moniker assignEntity = (Moniker)context.InputParameters.Properties["Target"];
opportunityid = assignEntity.Id.ToString();
}
break;
case "Delete":
if (context.InputParameters.Properties.Contains("Target"))
{
Moniker monikerentity = null;
monikerentity = (Moniker)context.InputParameters.Properties[ParameterName.Target];
opportunityid = monikerentity.Id.ToString();
}
break;
}
}

MessageName Class (CrmHelpers)

AddItem
AddMember
AddMembers
AddMembersByFetchXml
Assign
Book
Clone
CompoundCreate
Create
Delete
DeliverIncoming
DeliverPromote
ExecuteWorkflow
ExecuteWorkflow
GrantAccess
Handle
Lose
Merge
ModifyAccess
RemoveItem
RemoveMember
RemoveMembers
RemoveMembersByFetchXml
Reschedule
Retrieve
RetrieveExchangeRate
RetrieveMultiple
RetrievePrincipalAccess
RetrieveSharedPrincipalsAndAccess
RevokeAccess
Route
Send
SetState
SetStateDynamicEntity
Update
Win

Happy Plug-in coding!

David Fronk
Dynamic Methods Inc.

6 comments:

hyper said...

Thank you very much, you have saved my time and my nerves!

Dynamic Methods said...

Glad to see it was useful.

David Fronk
Dynamic Methods Inc.

Anonymous said...

Wonderful

Anonymous said...

Great effort. Saved me heaps of debugging.

hyper said...

There is no more "Lost" message, but "Lose".

Dynamic Methods said...

Hyper,

Thanks for catching that, I've updated the post accordingly.

David Fronk
Dynamic Methods Inc.

Post a Comment