Using Workflows to Calculate Field Values

Dynamics CRM does not have a built-in “calculated” field type, but you can use workflows and the Multiply by and Set to operators to get something close to it. For example, the Opportunity entity has built in attributes “Est. Revenue” (attribute name “estimatedvalue”) and “Probability” (attribute name “closeprobability”). Suppose you wanted a “probability-weighted” revenue forecast. You can create a custom attribute, and then use a workflow to multiply the built-in fields by each other to update the custom attribute. Here are the three attributes I will use to illustrate, all on the Opportunity entity:

  • Est. Revenue (entity name estimatedvalue, type money)
  • Probability (entity name closeprobability, type int)
  • Weighted Revenue. This is the custom attribute. Name it what you like and give it a type of money. 

After adding the custom attribute, we need to build a workflow to update its value with the product of the “Est. Revenue” and “Probability” attributes. The Multiply by operator works by multiplying the current value of a field by a single numeric value. What this means is that you cannot enter a formula as you might think. My inclination was to have a workflow update the weighted revenue value with something like this:
   =(est. revenue)*(probability)

Unfortunately it’s not quite that easy! Since the Multiply by operator only allows you to update the current value by multiplying it by something else, the workflow needs to perform three consecutive Update Record actions:

  1. Update weighted revenue with the current value of Est. Revenue.
  2. Update its new value by multiplying it by Probability.
  3. Update its new value by multiplying it by .01 (since Probability is an integer value!)

The next figure shows a simple example of this, with three Update actions generously commented to make it somewhat self-documenting.
 calculatedfield1

The next figure illustrates how to configure the Update Record action for step 2. In step 1, the Weighted Revenue field was updated with the value of Est. Revenue, so in step 2 we need to multiply it by Probability:

calculatedfield2

 
After that, the Update Record action in step 3 will account for the fact that Probability is an integer (e.g., 50% is represented by the integer value 50). To do that, multiply by .01, as we illustrate in the next figure.
 calculatedfield3
Here are a few comments and suggestions in case you need to implement functionality like this:

  • The reason it takes three Update Record actions to perform the calculation is that you’re always updating the current value of the field, and you can only perform one operation at a time. It would be better if you could use formulas, but you can’t. 
  • You can imagine a more complicated calculation might take many more Update Record actions. This might become clunky or impractical in the workflow approach I discussed here. If it does, it might turn out that using client-side (Jscript behind form events) is a better approach. While some people might argue that it’s ALWAYS better to use the client-side script approach, I don’t agree with that. For a problem like the one we’re trying to solve here, you can use either, and which one is better depends on your requirements…as well as your skill-set!
  • Another disadvantage of the workflow approach to calculating fields is the latency problem: Dynamics CRM workflows run asynchronously, one implication of which is that calculated fields will take some time to update on the form, generally from 15 to 30 seconds. If you use the workflow approach, you will probably want to take the calculated field off the form, or maybe “hide it” on another tab.
  • How should a workflow like this one be triggered? You might need to run it manually for records that were created before you implemented it. On an ongoing basis you might want to run it when a new record is created, or when the Est. Revenue or Probability attributes change. Again, the specifics will be determined by your business requirements. This figure shows what would be a reasonable configuration of an automatic workflow to do this:

calculatedfield4

11 Comments »

  1. Saritha Said,

    May 12, 2009 @ 10:40 am

    Nice post! I have tried to use workflows to do similar calculations but the users DO NOT like it. They want the calculated value to be populated as soon as they tab out of the source fields. So, in such cases, its always useful to do it in onchange event (Javascript).

  2. Richard Knudson Said,

    May 13, 2009 @ 1:15 pm

    You’re right: it has its uses, but when you need a nice responsive UI it’s not the best approach. One advantage of the workflow approach, however, is that I could make the workflow available on demand and then run it manually against existing records to fix them up if necessary. (I don’t think there’s any out of the box way to run jscript with a workflow, which would be nice sometimes.)

  3. Jürgen Said,

    May 27, 2010 @ 7:06 am

    Thanks a lot for this helpful tutorial! It is actually surprising how much it takes to achieve a relatively simple task in MS CRM :-)

  4. Richard Knudson Said,

    May 27, 2010 @ 7:20 am

    Hi Jurgen,

    Thanks for your comment. Actually, that wasn’t the takeaway I was going for! :-) Most people would use JavaScript to do this and that is not only easier to implement but it also happens instantaneously without the asynch workflow delay. As I mentioned in a previous comment, the only place where this approach would be the right one to take is if you needed to fix up records already in the database. For that, the workflow approach would probably be the way to go.

    Some things workflows are better for, some things are best done with form script/JavaScript. Best tool for the job, right?

  5. Nici Mahlandt Said,

    September 29, 2010 @ 2:00 pm

    Yeah you. After getting a LinkedIn update I couldn’t believe this is exactly what I had been asked to accomplish – but I hadn’t used Workflows (and boy are they convoluted with the click here and there). So What you showed above works perfectly – but I have two questions (again, being the luddite here). Do I always have to run the workflow manually against opps or will they update at some point (I ran them manually to see everything work – even though I set the select attributes as shown on the last screenshot). And second – is it possible to do this automatically with an onchange….and if so, do you happen to have one handy? Using same three fields…Est. Revenue, Probability, and Weighted Revenue. Thank you – and anyone else – for your help.

  6. Hjalti Said,

    May 11, 2011 @ 3:29 pm

    I must say I love this site. It helps me every time. Thank you for your awesome contribution!

  7. Len Kamerman Said,

    May 13, 2011 @ 9:35 am

    Thanks Richard – this helped me get math done via a workflow. In my case I can’t use Jscript because Mobile Express needs the calculation to happen as well. Might use both though – so it happens for full clients right away and workflow will run regardless so it catches the Mobile Express usage.

    Thanks,
    Len

  8. Mike Said,

    January 16, 2012 @ 12:58 pm

    Worked great, thanks!!

  9. Meri Said,

    January 24, 2012 @ 2:14 pm

    Hi Richard,

    Maybe I’m missing something but I’m not seeing that this is possible in CRM 2011. “Set to”, “Append with”, and “Clear” are the only operators I have. Thanks in advance.

  10. Meri Said,

    January 24, 2012 @ 2:33 pm

    Never mind. The fields have to be decimal type, not text…

    http://community.dynamics.com/product/crm/crmtechnical/b/crminogic/archive/2011/10/15/different-operators-used-with-workflow.aspx

  11. Richard Knudson Said,

    January 27, 2012 @ 6:49 am

    Yes, I was just about to say that. :-) Thanks for the link to that article — very helpful. And thanks for reading!

    Richard

Leave a Comment