function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael DelyMichael Dely 

Checkbox Validation Rule (But Trigger to Uncheck)

I have a validation rule in effect where a field needs to be checked off in order to save the record.  It acts as an acknowledgment.  After it is checked and the record saved, I would like the checkbox to uncheck itself.  Is there a trigger I can implement that would uncheck the box after completion, however not interfere with the validation rule that enforces the checkbox to be checked when saving.  Appreciate your help with this!

Thanks,
Mike
PrabhaPrabha
This is very important read, most of us know it by heart:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm

This means, you can't do this for the following reasons:
  1. The order says, Validations>>before triggers>>Validations again.
  2. You have a validation to prevent if the check box is false (Example, you are saving a record and your check box, Acknowledge, is true)
  3. You can only modify the context data in Before triggers (Lets say you unchecked Acknowledge in a trigger code)
  4. Your validations fire again finds Acknowledge checkbox and throws the error.
So you can't do this with the cobination of Validation and Trigger.But you are not hopeless, There is actually coupleof ways,


First One: Create a DateTime Field behind the scenes and Update the two fields within Workflow/ process builder.
Validation rule:
!Acknowldge__c && ISCHANGED( Acknowledge_systemField__c )
1. This one will fire on your First Validation if the check box is not checked.
2. Process builder will update the two fields, Acknowldge to false and datetime to now(). process builder
3. Validations will run again, but this time, it will not fire since you updated the datetime field.
I verified this, it is working.

Second one: If you are NOT A BIG FAN OF PROCESS BUILDERS LIKE ME, Dump the Validation, move that enite logic to Before triggers, Here is what you do:
Validate that checkbox in code and throw error with AddError(customlabel).
 
trigger MyTrigger on Account (before update){ 

for(Account c:trigger.new){

 if(!c.Acknowledge__c && Trigger.OldMap.get(c.id).Acknowledge__c==false){ 
c.AddError(label.AcknowledgeErrorMsg); 
}
else {
c.Acknowledge__c = false;
}
 }
 }

HTH,
Prabhan​​​​​​​