Microsoft Dynamics CRM 4.0 - Editing Entities in Workflow

Friday, 22 August 2008 22:52 by RanjanBanerji

So I am now working with Microsoft Dynamics CRM 4.0.  I had never heard of this product until a few weeks ago when I was asked to build a workflow for it.  CRM uses Windows Workflow which unfortunately I know very little about.  So this is going to be quite a learning experience.

Like most recent Microsoft products there is very limited documentation on the CRM API and a lot of what is there is in my opinion just not very good.  But anyway, this is not about how MS sucks at delivering products.  There are sufficient number of bloggers out there taking care of that.

One of the issues that took me a few hours to figure out is how do I edit an entity that is being used in a workflow.  This is easy if you are dealing with any of the "out of the box" entities that come with CRM.  These include entities such as account, incident, contact etc.  In such a situation in the Execute method for the workflow activity you can write the following code:

protected overrideActivityExecutionStatus Execute( ActivityExecutionContext executionContext ) {

    IContextService contextService = (IContextService)executionContext.GetService( typeof( IContextService ) );
    IWorkflowContext context = contextService.Context;

    ICrmService crmService = context.CreateCrmService();

    WhoAmIRequest systemUserRequest = newWhoAmIRequest();
    WhoAmIResponse systemUser = (WhoAmIResponse)crmService.Execute( systemUserRequest );


    incident currentIncident = (incident)crmService.Retrieve( EntityName.incident.ToString(), context.PrimaryEntityId, newAllColumns() );
    currentIncident.productserialnumber = "999";
    //updating the case entity
  
crmService.Update( currentIncident );
   
    return base.Execute( executionContext );
}

CRM provides strongly typed objects for all the "out of the box" entities.  This makes working with entities very easy.  But what if you extended (customized) one of these entities?  What if you added a new attribute to the incident entity?  How would you modify that new attribute/property?  The strongly typed objects do not modify themselves just because you added a few new attributes.  Unfortunately in addition to their properties they do not support some sort of a key-value pairing so you can do something like currentIncident[ "MyNewProperty"] = someValue;

What you have to do is write a whole bunch of extra code when it comes to custom entities.  This code is available in the CRM SDK as a sample but here goes:

protected override ActivityExecutionStatus Execute( ActivityExecutionContext executionContext ) {    
    IContextService contextService = (IContextService)executionContext.GetService( typeof( IContextService ) );    
    IWorkflowContext context = contextService.Context;    
    ICrmService crmService = context.CreateCrmService();    
    WhoAmIRequest systemUserRequest = new WhoAmIRequest();    
    WhoAmIResponse systemUser = (WhoAmIResponse)crmService.Execute( systemUserRequest );        
    //Retrieve the Incident Entity but as a Dynamic Entity since we have customized the incident     
    //entity by adding additional attributes    
    DynamicEntity dynamicIncident;    {        
        //Create the target       
        TargetRetrieveDynamic retrieveTarget = new TargetRetrieveDynamic();        
        retrieveTarget.EntityId = context.PrimaryEntityId;        
        retrieveTarget.EntityName = EntityName.incident.ToString();        
        //Create a request        
        RetrieveRequest retrieveRequest = new RetrieveRequest();        
        retrieveRequest.ColumnSet = new AllColumns();        
        retrieveRequest.ReturnDynamicEntities = true;        
        retrieveRequest.Target = retrieveTarget;        
        //Execute the request        
        RetrieveResponse retrieveResponse = (RetrieveResponse)crmService.Execute( retrieveRequest );        
        //Retrieve the Loan Application Entity        
        dynamicIncident = retrieveResponse.BusinessEntity as DynamicEntity;    
    }    
    //Make random value changes    
    dynamicIncident[ "productserialnumber" ] = "555_" + System.Guid.NewGuid().ToString();    
    dynamicIncident[ "new_productnumber" ] = "999_" + System.Guid.NewGuid().ToString();    
    dynamicIncident[ "new_safetystocklevel" ] = new CrmNumber( 540 );    
    //updating the entity    
    //Create an update target    
    TargetUpdateDynamic updateDynamicTarget = new TargetUpdateDynamic();    
    // Set the properties of the target.    
    updateDynamicTarget.Entity = dynamicIncident;    
    // Create the update request object.    
    UpdateRequest updateRequest = new UpdateRequest();    
    // Set request properties.    
    updateRequest.Target = updateDynamicTarget;    
    // Execute the request.    
    UpdateResponse updatedResponse = (UpdateResponse)crmService.Execute( updateRequest );    
    return base.Execute( executionContext );
}

Not as trivial anymore is it?

 

 

Categories:   MS Dynamics CRM
Actions:   E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed