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
Carolina W 2Carolina W 2 

Run Trigger only by Flow

I want to call a Before Update Trigger only when my Flow is running.
I created a checkbox field called "Run_code__c". The standard value is false.
My code:
for(Account acc:trigger.new){
   if(acc.run_code__c){ //Do stuff } 
   else { acc.addError('MsgError'); }

But how can I change run_code__c to "true" in the flow if the trigger will block it?
There is another way to solve it?
My goal is to run the Trigger Before Upadate only when my Flow is running.
Best Answer chosen by Carolina W 2
ShirishaShirisha (Salesforce Developers) 
Hi Carolina,

Greetings!

When the record is updated by the automated user(fire/process) then there is a chance that the trigger will not fire.

So,you would need to use the apex class with @incovable method to achieve this requirement.Otherwise,you can refer the below thread for the suggestion:

https://salesforce.stackexchange.com/questions/333565/bypass-before-update-trigger

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

 

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Carolina,

Greetings!

When the record is updated by the automated user(fire/process) then there is a chance that the trigger will not fire.

So,you would need to use the apex class with @incovable method to achieve this requirement.Otherwise,you can refer the below thread for the suggestion:

https://salesforce.stackexchange.com/questions/333565/bypass-before-update-trigger

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

 
This was selected as the best answer
Carolina W 2Carolina W 2

Thanks, Shirisha!
U helped me a lot!

What I have done:

Class

public class MyClass{

   public static boolean runCode = false;

   public static void Method1 (List<Account> accs){

       for(Account acc:accs) {  
          if(runCode ==true){ //Do stuff }   
         else { acc.addError('You can only change the record by the  botton'); }
       }
    }

   @InvocableMethod
   (label='Autorize Flow' description='Make Flow update the record' category='Account')
    public static void Method2 (){
       
      runCode =true;
     }
}
Trigger
 
trigger MyTrigger on Account (Before Update) {

   MyClass.Method1(trigger.new);

}

P.s: I deleted "Run_code__c". I'm not using anymore.

Thanks!
 

Carolina W 2Carolina W 2
Complementing: 
So in the flow, before update the record, I put an Apex Action.