0

I have a trigger (Before Update Account) that have to work only if I click in my botton that is conected in a flow.

So I created a checkbox field called "Run_code__c". The standard value is false.

What I was thinking to do:
1- I set Run_code__c value to "true" in my flow and my trigger have to run
2 - Otherwise, if Run_code__c is false, my code won't run

But setting Run_code__c = true it's an update and the trigger will block it anyway haha

Do you have an Idea for my code only runs if the flow is running?

1
0

You can check for the value in the before update

for(Account acc:trigger.new){ if(acc.run_code__c){ //Do stuff } }

Or dependant on your org data size switch to flow and remove the trigger entirely

3
  • I want to do it, but my code is : for(Account acc:trigger.new){ if(acc.run_code__c){ //Do stuff } else{acc.addError('MsgError'} } And it have to work in a Before Update Trigger... How can I change run_code__c in the flow if the trigger will block it?
    – Caroline
    Feb 8 '21 at 0:14
  • you need to look at the trigger context as a part of it. trigger.isbefore && trigger.isupdate so then if run_code__c = true the before update will run - not convinced you should include the else on this as that will prevent your flow from completing. if you are doing this in a flow I'd be more included to not include the trigger and instead handle the process in the flow. that way you don't have to worry about the trigger
    – SilentBlob
    Feb 9 '21 at 10:46
  • I don't know if i understood, but I can't do it totally in the flow, because I have to block people to edit the record without clicking in the botton
    – Caroline
    Feb 10 '21 at 1:35
0

What I have done:

Class


   public class MyClass{

     public static boolean runCode = false;

     public static void Method1 (List 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.
So in the flow, before update the record, I put an Apex Action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.