Pages

Sunday, August 14, 2016

Salesforce: Validation Rule in Workflow and Process Builder

Based on Salesforce Triggers and Order of Execution "If the record was updated with workflow field updates, fires before update triggers and after update triggers one more time (and only one more time), in addition to standard validations. Custom validation rules, duplicate rules, and escalation rules are not run again."

This mean, when a user hit save button, all Validation Rules will fired, if nothing fit into custom validation rules defined, workflow will kick-in and if there is any field update, validation rules will not fired again.



Rule: Account Number should be not blank, but workflow able to set Account Number to blank.

Scenario: Initially Account Number is not blank, user edit the Account, and Workflow rule will fired with Field Update action to set Account Number to blank.

Let's debug this workflow:
37.0 APEX_CODE,DEBUG;APEX_PROFILING,DEBUG;CALLOUT,DEBUG;DB,DEBUG;SYSTEM,DEBUG;VALIDATION,DEBUG;VISUALFORCE,DEBUG;WORKFLOW,DEBUG
14:23:35.0 (82155)|USER_INFO|[EXTERNAL]|00528000002ILq8|abc@playground.com|Singapore Time|GMT+08:00
14:23:35.0 (114745)|EXECUTION_STARTED
14:23:35.0 (120056)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Account:0012800000gyUy6
14:23:35.0 (130074)|VALIDATION_RULE|03d28000000EPIz|Validation_Rule_Check_Account_Number
14:23:35.0 (228425)|VALIDATION_FORMULA|ISBLANK( AccountNumber )|AccountNumber=4
14:23:35.0 (237103)|VALIDATION_PASS
14:23:35.0 (269418)|CODE_UNIT_FINISHED|Validation:Account:0012800000gyUy6
14:23:35.0 (274425)|EXECUTION_FINISHED
14:23:35.30 (30037797)|USER_INFO|[EXTERNAL]|00528000002ILq8|abc@playground.com|Singapore Time|GMT+08:00
14:23:35.30 (30056259)|EXECUTION_STARTED
14:23:35.30 (30061600)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Account
14:23:35.30 (50813798)|WF_RULE_EVAL_BEGIN|Workflow
14:23:35.30 (50860348)|WF_CRITERIA_BEGIN|[Account: Ke 4 0012800000gyUy6]|Account Name not blank|01Q28000000Cc09|ON_ALL_CHANGES|0
14:23:35.30 (54819564)|WF_RULE_FILTER|[Account : Account Name not equal to ]
14:23:35.30 (54838482)|WF_RULE_EVAL_VALUE|Ke 4
14:23:35.30 (54843398)|WF_CRITERIA_END|true
14:23:35.30 (73750806)|WF_SPOOL_ACTION_BEGIN|Workflow
14:23:35.30 (73896415)|WF_FIELD_UPDATE|[Account: Ke 4 0012800000gyUy6]|Field:Account: Account Number|Value:null|Id=04Y280000002Bz8|CurrentRule:Account Name not blank (Id=01Q28000000Cc09)
14:23:35.30 (73918213)|WF_ACTION| Field Update: 1;
14:23:35.30 (73920533)|WF_RULE_EVAL_END
14:23:35.30 (88370151)|WF_ACTIONS_END| Field Update: 1;
14:23:35.30 (88385818)|CODE_UNIT_FINISHED|Workflow:Account
14:23:35.30 (88392301)|EXECUTION_FINISHED

legend:
  • Red highlight 03d28000000EPIz = Validation Rule Id
  • Orange highlight 01Q28000000Cc09 = Workflow Rule Id
  • Cyan highlight 04Y280000002Bz8 = Field Update action Id
After workflow action end, execution finished.


Let's convert above Workflow rule to Process Builder with Update Records action, it should be pretty straight forward, let see the result when we edit and save the Account from Salesforce UI:

Workflow Action Failed to Trigger Flow
The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 30128000000AgZ7. Contact your administrator for help.

Click here to return to the previous page. 

Instead of getting the same result, we will see above error message. But, admin who create the Process Builder / Flow will receive fault email with more detail information, such as:

An error occurred at element myRule_1_A1 (FlowRecordUpdate).
UPDATE --- UPDATE FAILED --- ERRORS : (FIELD_CUSTOM_VALIDATION_EXCEPTION) Validation Rule Account_Number is blank
________________________________________
This report lists the elements that the flow interview executed. The report is a beta feature.
We welcome your feedback on IdeaExchange.
Flow Details
Flow Name: Account_Name_not_blank
Type: Workflow
Version: 1
Status: Active
Flow Interview Details
Interview Label: Account_Name_not_blank-1_Account
Current User: ABC Playground (00528000002ILq8)
Start time: 8/14/2016 2:34 PM
Duration: 0 seconds
How the Interview Started
Admin Playground (00528000002ILq8) started the flow interview.
Some of this flow's variables were set when the interview started.
myVariable_old = 0012800000gyUy6AAE
myVariable_current = 0012800000gyUy6AAE
ASSIGNMENT: myVariable_waitStartTimeAssignment
{!myVariable_waitStartTimeVariable} Equals {!Flow.CurrentDateTime}
Result
{!myVariable_waitStartTimeVariable} = "8/14/2016 2:34 PM"
DECISION: myDecision
Executed this outcome: myRule_1
Outcome conditions: and
1. {!myVariable_current.Name} (Ke 4) Does not equal null
Logic: All conditions must be true (AND)
RECORD UPDATE: myRule_1_A1
Find all Account records where:
Id Equals {!myVariable_current.Id} (0012800000gyUy6AAE)
Update the records’ field values.
AccountNumber = 
Result
Failed to update records that meet the filter criteria.
________________________________________
Error Occurred: UPDATE --- UPDATE FAILED --- ERRORS : (FIELD_CUSTOM_VALIDATION_EXCEPTION) Validation Rule Account_Number is blank

legend:
- Red text = Process Builder API Name
- Blue text = Validation Rule error message

Note: 301 prefix is refer to an object call "InteractionDefinitionVersion", but you cannot easily see what is this Id refer to. This Id actually can be refer to Process Builder or Flow. The workaround is using Flow Designer (remember that Process Builder is using Flow as it engine) to check if the Id is referring to which Process Builder or Flow, follow this URL https://na3.salesforce.com/designer/designer.apexp#Id=30128000000AgZ7, change na3 to your salesforce instance. In my case, I see Flow Designer below, it show the Process Builder is "Account Name not blank".



Let's see this Process Builder debug log:

37.0 APEX_CODE,DEBUG;APEX_PROFILING,DEBUG;CALLOUT,DEBUG;DB,DEBUG;SYSTEM,DEBUG;VALIDATION,DEBUG;VISUALFORCE,DEBUG;WORKFLOW,DEBUG
14:34:47.0 (558162)|USER_INFO|[EXTERNAL]|00528000002ILq8|abc@playground.com|Singapore Time|GMT+08:00
14:34:47.0 (592212)|EXECUTION_STARTED
14:34:47.0 (598100)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Account:0012800000gyUy6
14:34:47.0 (606907)|VALIDATION_RULE|03d28000000EPIz|Validation_Rule_Check_Account_Number
14:34:47.0 (776191)|VALIDATION_FORMULA|ISBLANK( AccountNumber )|AccountNumber=4
14:34:47.0 (781498)|VALIDATION_PASS
14:34:47.0 (804187)|CODE_UNIT_FINISHED|Validation:Account:0012800000gyUy6
14:34:47.0 (809205)|EXECUTION_FINISHED
14:34:47.59 (59552281)|USER_INFO|[EXTERNAL]|00528000002ILq8|abc@playground.com|Singapore Time|GMT+08:00
14:34:47.59 (59569571)|EXECUTION_STARTED
14:34:47.59 (59575357)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Account
14:34:47.59 (82062195)|WF_RULE_EVAL_BEGIN|Workflow
14:34:47.59 (82101694)|WF_CRITERIA_BEGIN|[Account: Ke 4 0012800000gyUy6]|Account_Name_not_blank30128000000AgZ7|01Q28000000Cc0E|ON_ALL_CHANGES|0
14:34:47.59 (82345496)|WF_FORMULA|Formula:ENCODED:[treatNullAsNull]true|Values:
14:34:47.59 (82352535)|WF_CRITERIA_END|true
14:34:47.59 (97686928)|WF_SPOOL_ACTION_BEGIN|Workflow
14:34:47.59 (97718850)|WF_ACTION| Flow Trigger: 1;
14:34:47.59 (97721792)|WF_RULE_EVAL_END
14:34:47.59 (103758514)|WF_FLOW_ACTION_BEGIN|09L28000000Dx9D
14:34:47.105 (105390847)|USER_INFO|[EXTERNAL]|00528000002ILq8|abc@playground.com|Singapore Time|GMT+08:00
14:34:47.105 (105416019)|FLOW_CREATE_INTERVIEW_BEGIN|00D28000001GqzS|30028000000DADN|30128000000AgZ7
14:34:47.105 (328513776)|FLOW_CREATE_INTERVIEW_END|2218d9911a59768798c816adb579156879d9ea4-3fe3|Account Name not blank
14:34:47.328 (328991140)|USER_INFO|[EXTERNAL]|00528000002ILq8|abc@playground.com|Singapore Time|GMT+08:00
14:34:47.328 (329016464)|FLOW_START_INTERVIEWS_BEGIN|1
14:34:47.328 (356679048)|FLOW_START_INTERVIEW_BEGIN|2218d9911a59768798c816adb579156879d9ea4-3fe3|Account Name not blank
14:34:47.328 (357910736)|FLOW_START_INTERVIEW_END|2218d9911a59768798c816adb579156879d9ea4-3fe3|Account Name not blank
14:34:47.632 (632463208)|USER_INFO|[EXTERNAL]|00528000002ILq8|abc@playground.com|Singapore Time|GMT+08:00
14:34:47.632 (632488241)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Account:0012800000gyUy6
14:34:47.632 (632503881)|VALIDATION_RULE|03d28000000EPIz|Validation_Rule_Check_Account_Number
14:34:47.632 (632613125)|VALIDATION_FORMULA|ISBLANK( AccountNumber )|AccountNumber=null
14:34:47.632 (632625147)|VALIDATION_FAIL
14:34:47.632 (632678806)|CODE_UNIT_FINISHED|Validation:Account:0012800000gyUy6
14:34:47.328 (634577332)|FLOW_ELEMENT_ERROR|UPDATE --- UPDATE FAILED ---  ERRORS :  (FIELD_CUSTOM_VALIDATION_EXCEPTION) Validation Rule Account_Number is blank, |FlowRecordUpdate|myRule_1_A1
14:34:47.328 (663434606)|FLOW_START_INTERVIEWS_END|1
14:34:47.59 (663505014)|WF_FLOW_ACTION_ERROR|09L28000000Dx9D|30028000000DADN|Error executing flow: Account_Name_not_blank, FlowDefId: 30028000000DADN, FlowVersionId: 30128000000AgZ7
14:34:47.59 (663520003)|WF_FLOW_ACTION_ERROR_DETAIL|<b>An unhandled fault has occurred in this flow</b><br>An unhandled fault has occurred while processing the flow.  Please contact your system administrator for more information.
14:34:47.59 (663596389)|WF_FLOW_ACTION_END|09L28000000Dx9D
14:34:47.59 (663613069)|CODE_UNIT_FINISHED|Workflow:Account
14:34:47.59 (665545797)|EXECUTION_FINISHED

legend:
  • Red highlight 03d28000000EPIz = Validation Rule Id, this is the same validation rule
  • Green highlight 30128000000AgZ7= Interaction Definition Version Id, this is the Id show in error message (cannot open directly)
  • Orange highlight 01Q28000000Cc0E = workflow Id, (cannot open directly, because this is Flow)
  • Blue highlight 09L28000000Dx9D = Action Flow Id  (cannot open directly)
  • Yellow highlight 30028000000DADN = Flow Definition Id (cannot open directly)

If we analyze the log above, within Flow Action, it will call Validation Rule and this will stop Process Builder to update record.

Let's compare to a successful Process Builder with Record Update and Validation Rule.
37.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
15:24:54.0 (95867)|USER_INFO|[EXTERNAL]|00528000002ILq8|abc@playground.com|Singapore Time|GMT+08:00
15:24:54.0 (129750)|EXECUTION_STARTED
15:24:54.0 (135441)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Account:0012800000gyUy6
15:24:54.0 (146785)|VALIDATION_RULE|03d28000000EPIz|Validation_Rule_Check_Account_Number
15:24:54.0 (309565)|VALIDATION_FORMULA|ISBLANK( Name )|Name=Ke 4
15:24:54.0 (314519)|VALIDATION_PASS
15:24:54.0 (334878)|CODE_UNIT_FINISHED|Validation:Account:0012800000gyUy6
15:24:54.0 (339757)|EXECUTION_FINISHED
15:24:54.30 (30239766)|USER_INFO|[EXTERNAL]|00528000002ILq8|abc@playground.com|Singapore Time|GMT+08:00
15:24:54.30 (30253350)|EXECUTION_STARTED
15:24:54.30 (30258265)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Account
15:24:54.30 (42750062)|WF_RULE_EVAL_BEGIN|Workflow
15:24:54.30 (42795063)|WF_CRITERIA_BEGIN|[Account: Ke 4 0012800000gyUy6]|Account_Name_not_blank30128000000AgZ7|01Q28000000Cc0E|ON_ALL_CHANGES|0
15:24:54.30 (43029449)|WF_FORMULA|Formula:ENCODED:[treatNullAsNull]true|Values:
15:24:54.30 (43036888)|WF_CRITERIA_END|true
15:24:54.30 (44033222)|WF_SPOOL_ACTION_BEGIN|Workflow
15:24:54.30 (44056720)|WF_ACTION| Flow Trigger: 1;
15:24:54.30 (44058614)|WF_RULE_EVAL_END
15:24:54.30 (44682997)|WF_FLOW_ACTION_BEGIN|09L28000000Dx9D
15:24:54.46 (46193540)|USER_INFO|[EXTERNAL]|00528000002ILq8|abc@playground.com|Singapore Time|GMT+08:00
15:24:54.46 (46218553)|FLOW_CREATE_INTERVIEW_BEGIN|00D28000001GqzS|30028000000DADN|30128000000AgZ7
15:24:54.46 (47747434)|FLOW_CREATE_INTERVIEW_END|1940b7d42414e1d9dcfb404801f715687e6cde0-3f6f|Account Name not blank
15:24:54.48 (48073487)|USER_INFO|[EXTERNAL]|00528000002ILq8|abc@playground.com|Singapore Time|GMT+08:00
15:24:54.48 (48088578)|FLOW_START_INTERVIEWS_BEGIN|1
15:24:54.48 (48766530)|FLOW_START_INTERVIEW_BEGIN|1940b7d42414e1d9dcfb404801f715687e6cde0-3f6f|Account Name not blank
15:24:54.48 (49602259)|FLOW_START_INTERVIEW_END|1940b7d42414e1d9dcfb404801f715687e6cde0-3f6f|Account Name not blank
15:24:54.91 (91366282)|USER_INFO|[EXTERNAL]|00528000002ILq8|abc@playground.com|Singapore Time|GMT+08:00
15:24:54.91 (91385208)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Account:0012800000gyUy6
15:24:54.91 (91397228)|VALIDATION_RULE|03d28000000EPIz|Validation_Rule_Check_Account_Number
15:24:54.91 (91481561)|VALIDATION_FORMULA|ISBLANK( Name )|Name=Ke 4
15:24:54.91 (91487538)|VALIDATION_PASS
15:24:54.91 (91509480)|CODE_UNIT_FINISHED|Validation:Account:0012800000gyUy6
15:24:54.48 (109154474)|FLOW_START_INTERVIEWS_END|1
15:24:54.30 (110713941)|WF_FLOW_ACTION_END|09L28000000Dx9D
15:24:54.30 (110757222)|WF_RULE_EVAL_BEGIN|Workflow
15:24:54.30 (110768818)|WF_SPOOL_ACTION_BEGIN|Workflow
15:24:54.30 (110774246)|WF_ACTION| None
15:24:54.30 (110776310)|WF_RULE_EVAL_END
15:24:54.30 (110844992)|WF_ACTIONS_END| Flow Trigger: 1;
15:24:54.30 (110852765)|CODE_UNIT_FINISHED|Workflow:Account
15:24:54.30 (112112867)|EXECUTION_FINISHED




Reference: