0

I have a FLOW that works successfully when Opportunity Product (OpportunityLineItem) records are created or update but not when one is removed. This is because a FLOW even when set to "When a Record is Deleted" runs as "before delete" and I need it to fire as "after delete".

It's a Auto Launched flow that is called as a Sub Flow when Opportunity Product is created or updated. A single text variable is passed through to it called opportunityID representing the 18 digit ID of the OpportunityLineItem.OpportunityId

The flow then does a Get Record to gather in all the child Opportunity Produccts and performs a series of calculations before writing the answer back onto the Opportunity record. It does this very successfully.

My attempt to write an after delete trigger is below.

trigger OpportunityProductAfterDelete on OpportunityLineItem (after delete) {
    
    MAP<String,Object> inputs = new Map<String,Object>();
    for (OpportunityLineItem OPLI : trigger.old){
        inputs.put(OPLI.opportunityId,OPLI);
    }
    
    Flow.Interview.apiFlowName myFlow = new Flow.Interview.apiFlowName(inputs);
    myFlow.Start();
    
    
}

I can see from the LOG files that the Flow is indeed running but it doesn't do anything. I suspect the problem is with my inputs.

Possibly the FLOW which takes a text variable representing OpportunityID should instead be taking a record variable representing Opportunity.

Possibly my APEX code is just wrong.

Appreciate any assistance.

1
  • 1
    I worked out how to fix it in FLOW by adding an additional input value of Variable(TEXT) called deletedID and when the FLOW running on Before Delete fires it passes through the value of the deleted record which is then excluded from the results. Feb 18, 2023 at 3:47

2 Answers 2

0

The specific error is in the construction of the inputs Map

The map is of the form string => object

where

  • string is the case sensitive name of a Flow variable declared as available for input
  • object is the value for that variable (agreeing in type)

example, a flow has a variable declared for input called recordId, the map you construct should look like

`recordId` => someId of the Opportunity Product

In your case, you are creating a Map of ids => objects. But Ids are values, not input variable names.

Now, we come to the issue of bulkification. Your trigger is processing a collection of deleted OpportunityLineItems and a single Flow Interview tends to be written from the standpoint of a single record, not a collection of records.

There is a discussion on bulkifying calls to auto-launched flows here.

2
  • So if I understand correctly, and I'm not sure I do, then my FLOW instead of taking an input of a TEXT should take an input of an OBJECT so that when I create MAP<String,Object> I can then populate it as MAP<'flow-variable',Sobject> Do I have this correct? Feb 19, 2023 at 21:45
  • personally, I would make the map look like 'recordId' => 'someOpportunityLineItemId` and then call the start() method. Because you are using a trigger, you'll need to call the start() method in a loop n times, where n is the trigger size
    – cropredy
    Feb 20, 2023 at 18:36
0

This is the code that fixed my problem.

Firstly I didn't understand using fat arrows in APEX to assign map values. I'd only seen this in javascript before so initially I didn't understand Cropredy's answer.

Secondly I didn't realise that although APEX is not case sensitive when you're passing inputs to a FLOW it is case sensitive.

trigger OpportunityLineItemDeleteTrigger on OpportunityLineItem (after delete) {
    for (OpportunityLineItem oli : Trigger.old) {
        
        String opportunityId = oli.OpportunityId;
        String deletedId = oli.Id;

        // Create a new flow interview
        Flow.Interview.apiNameOfFlow myFlow = new Flow.Interview.apiNameOfFlow(new Map<String, Object>{
            'opportunityID' => opportunityId,  //The input value to the FLOW is case sensitive
            'deletedID' => deletedId           //The input value to the FLOW is case sensitive
        });
        
        // Start the flow interview
        myFlow.start();
        
    }
}
1
  • 1
    yep - this looks good
    – cropredy
    Feb 20, 2023 at 18:37

You must log in to answer this question.

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