Back in the dark ages, like with CRM 3.0, we were often faced with writing .NET code to provide certain enhanced data manipulation capabilities as either Callouts or Workflow Assemblies.  CRM 4.0 totally changes that mind set – and as a solution developer, you really need to keep that in mind before you jump off into the depths of Visual Studio and the CRM SDK.

An Example

To illustrate my point, let's take the PostInvoicePlugin example from the CRM SDK ( v4.0.5 ).

Note: this example is found in the SDK folder: sdkserverfullsamplepluginpostinvoice

The purpose of this sample code is to show you how to update a custom field on the Account record whenever a user closes out an Invoice as Paid.  The plugin checks the state of the Invoice, and if Paid, it will update the value of a custom field on Account, using the following code:

// If the Total Invoiced exists, update it... otherwise set it
if (parentAccount.Properties.Contains("new_totalinvoiced"))
{
    ((CrmMoney)parentAccount.Properties["new_totalinvoiced"]).Value += totalAmount;
}
else
{
    parentAccount.Properties.Add(new CrmMoneyProperty("new_totalinvoiced",
                                 new CrmMoney(totalAmount)));
}

Pretty cool, huh?

 

An Alternative

If you haven't spent much time reviewing CRM 4.0 workflows, you should.  You can perform some amazing things without ever writing code.  Let's perform the exact same steps using CRM 4.0 workflow.

First, we create a workflow based on the Invoice Entity that has the following properties:

image

Step 1:

Create a Check Condition using the following information:

image

Step 2:

Add and Update Record step, on the Related Entity Customer (Account):

image

Step 3:

Click the Set Properties button, then set the Total Invoiced Attribute to the following:

image 

Note: Total Invoiced is a custom Money Field.

By using the Increment by Operator, we are able to increment any existing Total Invoiced amount by the amount found in the Invoice's Total Amount field.

Final Step:

Save and Publish this workflow.

Conclusion:

So, is there a difference between these techniques?  Not really.  Both accomplish the job in a very similar manner but the Workflow solution requires zero code maintenance and a normal user or administrator can create the workflow.

The biggest difference between the two techniques is in the timing of the actual update. Plugins can be executed either Synchronously or Asynchronously.   Workflows function in an asynchronous manner.

The Synchronous operation will modify the data stream as it is being saved to the database, which can introduce a delay in the user's experience, but will provide results back to the user in a quicker fashion.

Asynchronous operations will happen shortly after the data has been saved which will not impact the user, but which may result in a slight delay between the time the user saved the record and when the value will be updated.

If you can live with the time delay, I feel that workflow is the way to go, in most cases.  If you can't, then download the SDK and start working through their examples.

 

9 Responses to Enhancing CRM 4: Workflows vs. Plugins

  1. Mike Mc says:

    This is really good but how do you use the plugin system with custom entities/fields? In crm 3.0 you could do customentity.customfield, i cant seem to get CRM 4.0 to compile a plugin based off the web service since it does not contain the iplugin class.

  2. mitch says:

    Mike,

    It sounds like you have not refreshed your web reference after creating your custom entity. Try that and see it it helps. That is if you are using the normal CRM Web Service.

    But, if you're talking about using the Microsoft.Crm.Sdk, and Microsoft.Crm.SdkTypeProxy, you're going to have to use a DynamicEntity to reference the custom Entity and Attributes.

    Mitch

  3. jz says:

    Mitch, when you use dynamicentity, you don't get the strongly-type object rite?

  4. mitch says:

    that is correct.

  5. Dennis says:

    I would say that the plugin is much stronger if you want CRM to interact with external applications/3rd-party databases.

    From that point of view (as a Solution Developer), you are better off using plugin's since you can use those always.

    Yes, workflows can be created more quickly and can be maintained a lot easier than plugin's, but the plugins are more versatile.

  6. Dustin says:

    Thanks, I was not sure if I was incorrect by trying to increment a loop counter custom int attribute for a workflow I'm working on. My CRM 4.0 deployment will not increment and update the field. Any ideas?

  7. Z says:

    Hi,

    this is probably a very stupid question but do plugins trigger if i use a custom web application to update an entity in crm using the sdk or would that only trigger a workflow? if a plugin would trigger would it be a sync plugin or async plugin.

    Thanks!
    Z

  8. Vinay says:

    Can u provide more example on workflow on my E-mail id.I am new user trying to understand the concept of Workflow.Help me out.

  9. [...] Enhancing CRM: Workflow vs Plug-ins:  Plug-ins can be executed either synchronously or asynchronously.   Workflows function in an asynchronous manner. [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>