1

I'm pretty sure this cannot be done, but just asking in case someone knows. I want to set a static Apex class variable in my flow, so that when my flows does an update and an apex trigger runs, the static variable is set to the value I want it to be. Can't see anything in the documentation that says I can do this.

2

1 Answer 1

1

So I tried this.

  1. Created a custom object just for testing this!
  2. Created a Record Flow (That runs after triggers)
  3. Created triggers to print static variable.
  4. Called an Invocable apex Class
  5. I have set the static variable inside the apex class
  6. update the same record, so it would call the triggers again.
  7. Now See the debug logs. debugs
    public class hamInvocableClass {
  @InvocableMethod(label='set static value' description='sets the static value' category='stackXchange')
  public static void runRajaRun(List<ID> ids) {
      GlobalUtils.setFrom = 'From Flow';
      system.debug('GlobalUtils.setFrom →' + GlobalUtils.setFrom);
      Request reqInfo = Request.getCurrent(); 
      String currentRequestId = reqInfo.getRequestId(); 
      system.debug('currentRequestId →' + currentRequestId);
      Quiddity currentType = reqInfo.getQuiddity(); 
      system.debug('Quiddity →' + currentType);
      update new hamayoun__c(id = ids[0]);
  }
}

Trigger:

    trigger hamayounTrigger on hamayoun__c (before insert, after insert, before update, after update) {
    if(trigger.isBefore){
        
      system.debug('From Before :: '+GlobalUtils.setFrom);
      Request reqInfo = Request.getCurrent(); 
      String currentRequestId = reqInfo.getRequestId(); 
      system.debug('currentRequestId →' + currentRequestId);
      Quiddity currentType = reqInfo.getQuiddity(); 
      system.debug('Quiddity →' + currentType);
    }
    if(trigger.isAfter){
        
      system.debug('From After :: '+GlobalUtils.setFrom);
      Request reqInfo = Request.getCurrent(); 
      String currentRequestId = reqInfo.getRequestId(); 
      system.debug('currentRequestId →' + currentRequestId);
      Quiddity currentType = reqInfo.getQuiddity(); 
      system.debug('Quiddity →' + currentType);
    }
}

Static VAriable:

public class GlobalUtils {
    public static string setFrom; 
}

What am I trying to prove here! Even though we cannot access the apex variables directly in the flow, we can actually call an apex method and set the static variable and the rest of the apex will be able to read it.

I am sure it applies to screen flows wherever you have access to apex.

2
  • oh I forgot, notice the currentRequestId and Quiddity, may be that is useful for your scenario.
    – Prabhan
    Jun 29, 2023 at 20:38
  • 1
    Watch out for flows that pause; these will restart in a new transaction and your apex state will be lost.
    – Phil W
    Jun 29, 2023 at 20:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .