Saturday 8 June 2013

Work with Assign SDK message in CRM 2011 plugin - manage logic based on the Assignee Team (CRM 2011)

In the following example I use the Assign message in CRM 2011 to manage some custom logic based on the assignee Team.
The plugin is registered in Pre-Operation stage on the Task entity and uses early-bound entity classes.
However it is trivial reuse the same logic on another type of entity, use late-bound entity classes, or operate on other type of assignee.

protected void ExecutePreTaskAssign(LocalPluginContext localContext)
{
    if (localContext == null)
    {
        throw new ArgumentNullException("localContext");
    }

    IPluginExecutionContext context = localContext.PluginExecutionContext;
    IOrganizationService service = localContext.OrganizationService;

    try
    {
        //Check if context contains the "Target" parameter and that it
          is an EntityReference
        if (context.InputParameters.Contains("Target") && 
                     context.InputParameters["Target"is EntityReference)
        {
            EntityReference targetEntity =    
                     (EntityReference)context.InputParameters["Target"];
            //Check if the entity type is Task
            if (targetEntity.LogicalName != "task")
            { return; }

            //Check if context contains the "Assignee" parameter
            if (context.InputParameters.Contains("Assignee"))
            {
                EntityReference assigneeRef = 
                       (EntityReference)context.InputParameters["Assignee"];
                //Check if the "Assignee" is a team
                if (assigneeRef.LogicalName == "team")
                {
                    Team team;
                    //Retrieve the team with all properties
                    team = service.Retrieve(assigneeRef.LogicalName, 
                        assigneeRef.Id, new ColumnSet(true)).ToEntity<Team>();

                    //Put your logic here based on the retrieved Team informations
                }
            }
        }
    }

    catch (Exception e)
    {
        throw new InvalidPluginExecutionException("An error occured for 
                Assign plugin " + e.Message + e.InnerException);
    }
}


Hope it can be useful.
Happy CRM coding.

Rate this posting:
{[['']]}

No comments:

Post a Comment