1. Blog
    2. Custom Permissions, Validation Rules and Apex CPU Limits

    Custom Permissions, Validation Rules and Apex CPU Limits

    • We had a requirement recently to allow a subset of users (not aligned to specific profiles) to be able to move an Stage field forward but not backwards. We thought this was a great use case for the Custom Permissions as referenced by Brian Kwong aka @salesforceWizard here http://thewizardnews.com/2015/01/08/custom-permissions-the-joy-of-flexibility-in-validation-rules-and-workflow/ and @andyinthecloud here https://andyinthecloud.com/2015/01/14/creating-assigning-and-checking-custom-permissions/

      We had the Validation rule working, however as the feature was being used more and more by our customer, more and more data was being updated simultaneously (I won’t go into the details but suffice to say more that 100 records being updated in one DML, via a Trigger, Class and Helper Class was becoming common).

      One day we started to get Apex CPU limit Exceeded errors when records were being updated and we started to troubleshoot the problem. It was whilst reviewing Timeline on Debug logs that we observed that 50% of the time was being used in Validation.

      After lots of trial an error we established that it was the use of NOT($Permission.Move_Element_Status_Backwards) which was included in the Validation rule that caused the problems. We decided to verify this by doing the following:

      First test the very simple validation rule

      1. Create New Object
      2. Create Validation Rule 1 – Formula value = false – i.e it will never fail but I needed at least one validation rule to see in the debug logs
      3. Insert 100 records using any bulk method (I used Dataloader).

      When I analysed the results this is what we got:

    • Notice the total duration was only 14.91ms

      Next is to test the #Permission Validation Rule

      1. Disable Validation Rule 1
      2. Create Custom Permission – Test
      3. Create Validation Rule 2 – Formula value = $Permission.Test
      4. Enable Validation Rule 2
      5. Insert 100 records using any bulk method (I used Dataloader).

      When I analysed the results this is what we got:

    • Notice the total duration was a massive 3,811.39ms

      This is worrying and I’m currently trying to validate from Salesforce if this is a Known Issue or Bug or a Limitation of Custom Permissions.

      Feel free to join the conversation here on twitter

      Adendum
      Johan Yu https://twitter.com/simplysfdc has also tested the use of Custom Permissions by loading 1,000 Leads and observed a 70% increase in the time taken to load the data.