Search code examples
javascriptdynamics-crmworkflowmicrosoft-dynamicsdynamics-crm-online

Microsoft Dynamics CRM run script (or something similar) in background


I wrote a script that adds the values of field A and field B. It then writes the result in field C.

My problem now is that, as I'm using a script, it only runs if I the form is open. However, I need to run it whenever field A or field B changes, regardless whether the form is open or not. For example, if a workflow changes the value of field A in the background, I need the script to calculate the new value of field C in the background as well.

I know that scripts only run on forms. That's why I'm looking for an alternative for scripts. I am aware that I could normally solve this by using a workflow, but I can't access field A through the workflow (it's a calculated field).

Are there any other possibilities?


Solution

  • I find that for custom calculations the following approach is effective and doesn't require much effort to implement and mantain:

    • Create an ACTION (let's call it new_action) without any steps
    • Create a PLUGIN which does the math you want, register it to the new_action message
    • Identify all the Simple fields involved
    • Create a workflow for each entity where you found the fields, set it to run on Update of the Simple fields you identified in that entity
    • The workflows should all be the same and the only step would be EXECUTE ACTION -> new_action

    The end result is this behavior:

    Simple field involved in your math changes -> Workflow starts -> Action starts -> Plugin does the math

    NOTE: I usually make the action Unbound and "hand-craft" the data received by both the action and the plugin, but I glossed over this aspect because the approach itself stays the same.