0

I'm having some issues with a trigger which is calling a flow to do some updates to the records being inserted via the trigger.

I've set up a flow which takes a collection of leads as an input variable, loops over the leads and updates the status of the leads based on field values set earlier by the trigger calling the flow. The updated lead collection is then passed back to the trigger as an output variable.

I call this flow and set the trigger leads to the flow output collection as the last action in the logic. However, the changes that the flow does to the leads, are not commited to the database - Only the changes made explicitly in the trigger are commited.

The code which deos the call looks something like this:


    Public static void onBeforeInsert(List<Lead> leadsToUpdate){ 
       //Do some changes to the leads in the list

        List<Lead> leadsWithChanges = methodThatDoesChanges(leadsToUpdate);

        Map<String,List<Lead>> leadMap = new Map<String,List<Lead>>{'Leads' => leadsWithChanges};
        Flow.Interview flow = new Flow.Interview(leadMap);
        leadsToUpdate = flow.getVariableValue('Leads);
    }


Debugging the returned list does show the correct values for the leads.

I guess this has something to do with the references. And since it's before insert i dont have any ID's to do mapping with after the flow has updated the values..

Any ideas, is this even possible?

Thanks

2
  • Commenting since I don't know enough about flows, but I suspect your issue is when you're overwriting leadsToUpdate (final line). That step may not be necessary. before trigger contexts only have the "automatic update" behavior when you make changes to the SObject instances referenced in Trigger.new or Trigger.newMap (Trigger.new here, since this is before insert). You could test this by using 2 system.debug()s (one before the flow, one after) to print one field on one record that you know the flow modifies. If the debugs print different values, then the final line is unnecessary.
    – Derek F
    Jan 19, 2021 at 14:30
  • Thanks for the quick reply. I tried that out and it seems like the flow doesn't change the records referenced in the record collection (Both debug statements returned the same value). So that is a good reason for the change not being commited. according to the section "Record updater" in According to this blogpost from 2014 this should be possible, i havnen't been able to get more info about it though.. :/
    – Cnork
    Jan 19, 2021 at 14:43

1 Answer 1

1

First, the order returned from the Flow should be the same as the order in the trigger (assuming you passed them in that way); this means you don't need the Id values. Since arrays are ordered, you could copy the values back by index.

Second, because of "references," I would expect the changes to happen to the records even without assigning the list back; if it's not doing so, there might be something else wrong with your code before you got to that point.

Third, a Before Save Flow could eliminate the need for a trigger entirely, depending on what else you're doing before the Flow is triggered.

Fourth, all things being equal, a Record Triggered Flow could also work, it would just cause a recursive save, which for many organizations, is an acceptable performance hit.

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.