CRM 2013 Features: Using Real-time Workflow for Validation Rules

Discussion in 'Dynamics CRM' started by Bin Tran, Oct 22, 2014.

  1. Bin Tran

    Bin Tran Active Member Staff Member

    Real-time workflows are a new feature in CRM 2013. When I train on these the learning curve tends to be a gentle slope, the simple message is “you can now configure your workflows to run synchronously”. Those 9 words comprise 90% of what you need to know. That’s not to say this isn’t an awesome feature, its just easy to understand. Here are a few other things you should know and cool new usage scenario that you should consider…

    Execution Context
    Workflows (both asynchronous and real-time types) can now execute either in the context of the User or in the context of the Owner of the Workflow:

    Dynamcis CRM 2013 Workflow validation Rules 1.png

    If you define the workflow to execute as the owner of the workflow than the workflow can potentially perform actions the user does not have permission to – e.g. create certain records, or delete a record. The workflow will have the permissions of the Owner of the workflow rule which you can set higher than the permissions of the end users.

    If you define the workflow to execute in the context of the user then any records created or updated will reflect the user as the CreatedBy / ModifiedBy.



    Validation Rules
    I’ve had scenarios in the past where I have needed to perform a validation when a user attempts to update or delete a record. If the record in question has certain properties I would need to reject the user action. A common example is where you have external data that is synchronized with CRM and you need to prevent updates to those synchronized records, but not other records that are user-created – and these records reside in the same entity.

    In CRM 2011 I either did this via JavaScript or a pre-update/pre-delete Plug-in. A plug-in is more robust solution as it would fire regardless of how the update/delete was initiated. No, we can do this via a real-time workflow! Here’s how…

    In my system I have Account records that are either user-created or created externally from CRM and synchronized with CRM via an integration. I have an “Account Source” field which distinguishes between these two:

    Dynamcis CRM 2013 Workflow validation Rules 2.png

    My users should be able to delete Accounts that are flagged as “User-entered” but not those flagged as “Integration”. I can’t achieve this via the CRM Security Model as it does not support data-based access rules. I need to grant my users delete permissions and then overwrite their delete attempts when I need to block that action. I can do this with a Workflow process.

    Here’s the properties of my Workflow rule:

    Dynamcis CRM 2013 Workflow validation Rules 3.png

    Note I have configured this as a real time workflow:
    Dynamcis CRM 2013 Workflow validation Rules 4.png


    And I am triggering the Workflow to run Before the record is deleted:

    Dynamcis CRM 2013 Workflow validation Rules 5.png


    Here’s the conditions and actions:

    Dynamcis CRM 2013 Workflow validation Rules 6.png


    A simple condition that checks the value of the Account Source field. If the value is “Integration” then the proceeding Action fires. The Action is to Stop the Workflow with a status of Canceled. Stopping the Workflow has always been an available Action:

    Dynamcis CRM 2013 Workflow validation Rules 7.png


    But with workflows not able to run synchronously we can use this feature to block the user action that triggered the Workflow in the first place.

    When you add this Action to a Workflow there are 2 options to pick from. You either stop the Workflow as Succeeded or as Canceled:

    Dynamcis CRM 2013 Workflow validation Rules 8.png


    We need to stop the Workflow as Canceled in this scenario as that will block the delete from completing.

    Next to this dropdown box is a Set Properties button. If you click that you can add an Exception message that will pop to the user to explain why you have blocked their action:

    Dynamcis CRM 2013 Workflow validation Rules 9.png


    You can embed fields from the record or from related records just like you can when you are using workflow to create a record. In my case I just include some static text.

    Here’s the workflow in action. I select an Account with an Account Source value of “Integration” and click Delete:


    Dynamcis CRM 2013 Workflow validation Rules 10.png
     
  2. Loading...


  3. Bin Tran

    Bin Tran Active Member Staff Member

    I click through a couple of standard CRM warnings:

    Dynamcis CRM 2013 Workflow validation Rules 11.png


    And then the workflow kicks in and throws the exception that I configured:

    Dynamcis CRM 2013 Workflow validation Rules 12.png


    And my record remains:

    Dynamcis CRM 2013 Workflow validation Rules 13.png


    Let’s look now and how this type of validation approach works in an Update scenario. I tweak my workflow to run on Update instead:

    Dynamcis CRM 2013 Workflow validation Rules 14.png


    And I adjust the error message:

    Dynamcis CRM 2013 Workflow validation Rules 15.png


    Let’s test this out…

    I set the workflow to fire when the Account name changes. Let’s change that and a couple of other fields too:

    Dynamcis CRM 2013 Workflow validation Rules 16.png


    And then wait for the auto-save to fire. What you will see is a Business Process Error notification down in the Save section of the form:

    Dynamcis CRM 2013 Workflow validation Rules 17.png


    Clicking on that will reveal the error:

    Dynamcis CRM 2013 Workflow validation Rules 18.png

    Trying to force the save or navigating away from the record also results in the error popping.

    The nice thing here is you don’t lose your changes when you hit this error. You can’t save until you address the issue and none of the updated fields get updated. But if I remove the change from the Account Name I can then save successfully and the other fields that I was also editing get updated successfully.
     

    Attached Files:

Share This Page