Query Records by List of Ids in Flow

Advertisements

Last Updated on May 8, 2022 by Rakesh Gupta

Big Idea or Enduring Question:

How can you automatically delete open tasks related to opportunity and quote(s) when an opportunity is marked as closed lost? 

Objectives:

After reading this blog post, the reader will be able to:

  • Understand @InvocableMethod and InvocableVariable Annotation
  • Get sObject Type in Apex code
  • Pass the list of recordIds to the invocable apex
  • Call invocable Apex class that takes a list of ids
  • Hard delete records using apex 
  • Call invocable method from the flow

Business Use case

Arda Turan is working as a System Administrator at Gurukul on Cloud (GoC). He has received a requirement from the management that whenever an opportunity is updated with stage closed lost, then do the following: 

  • Delete open tasks on the current opportunity
  • Delete open tasks from related quotes

Automation Champion Approach (I-do):

While the Record-Triggered Flow is very flexible out of the box, there are a few business use cases that are not achievable using it. For example
  • The record-triggered flow doesn’t support IN clause in get element 
  • The record-triggered flow doesn’t allow us to hard delete record(s)
  • and much more
While the above business can be solved using various automation tools like Apex or Flow (by added Get Element inside Loop), we will use Record-Triggered Flow and call an Invocable Apex method.

To call an
Apex method, add the Call Apex 
action to your process and select an Apex class with a @invocable method Annotation. It means they allow us to extend the Salesforce Flow by writing Apex code that meets certain criteria and then invoking the Apex from our Flows. If the class contains one or more invocable variables, manually enter values or reference field values from a related record. @InvocableMethod Annotation supports bulk operations. 

Before discussing the solution, let me show you a diagram of a Process Flow at a high level. Please spend a few minutes going through the following Flow diagram and understand it. Let’s begin building this automation process.

Guided Practice (We-do):

There are 2 steps to solve Arda’s business requirement using Record-triggered Flow. We must:
  1. Create Apex class & Test class
  2. Salesforce Flow Steps:
    1. Define flow properties for record-triggered flow
    2. Add a text collection variable to store the record Ids
    3. Add a decision element to validate opportunity stage equals closed lost
    4. Add an assignment element to add opportunityId to the collection variable (Created in step 1.2)
    5. Add a get records element to find the related quotes 
    6. Add a decision element to check if quotes was found or not
    7. Add a loop element to retrieve records from the record collection variable (from step 1.5) 
    8. Add an assignment element to add quoteId to the collection variable (Created in step 1.2)
    9. Add action – call an Apex class to delete the open tasks for given Ids 

Step 1: Create an Apex class and Test class   

  1. Click Setup.
  2. In the Quick Find box, type Apex Classes.
  3. Clicks on the New button.
  4. Copy code from GitHub and paste it into your Apex Class. 
  5. Click Save.
Repeat the above steps and click the Test class. You can get the code from my GitHub repo. 
  • The getSObjectType method is used to obtain an sObject token from an ID. 
  • The emptyRecycleBin is used to delete records from the recycle bin immediately.

Step 2.1: Define Flow Properties

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows then click on the New Flow.
  4. Select the Record-Triggered Flow option, click on Create 
    1. Object: Opportunity
    2. Trigger Opportunity Flow When: A record is created or updated
    3. Set Entry Criteria
      1. Condition Requirements: None
    4. Optimize the Flow For Action and Related Records
  5. Click Done.

Step 2.2: Add a Collection Variable Text to Store Record Ids

  1. Under Toolbox, select Manager, then click New Resource to store the opportunity or quote(s) ids. 
  2. Input the following information: 
    1. ResourceType: Variable
    2. API Name: varT_recordIds
    3. Data Type: Text
    4. Default Value: {!$GlobalConstant.EmptyString}
    5. Check Allow multiple values (collection) 
    6. Check Available for Input
    7. Check Available for Output
  3. Click Done.

Step 2.3: Using Decision Element to Check if Opportunity is Closed Lost or Not

Now we will use the Decision element to check the StageName to ensure that it is equal to Closed Lost. 

  1. On Flow Designer, click on the + icon and select the Decision element. 
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Under Outcome Details, enter theLabel theAPI Name will auto-populate.
  4. Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!$Record.StageName}
      2. Operator: Equals 
      3. Value: Closed Lost

The reason why we would select the Yes checkbox for the question — Only if the record that triggered the flow to run is updated to meet the condition requirements — is to allow the Flow to execute the actions only if the record meets the criteria now, but the values that the record had immediately before it was saved didn’t meet the criteria. This means that these actions won’t be executed when irrelevant changes are made.

Step 2.4: Adding an Assignment Element to Assign the Opportunity Id to the Collection Variable 

  1. On Flow Designer, below the Closed Lost node, click on the +icon and select the Assignment element. 
  2. Enter a name in the Label the API Name will auto-populate.
  3. Set Variable Values
    1. Row 1:
      1. Field: {!varT_Ids}
      2. Operator: Add
      3. Value: {!$Record.Id}
  4. Click Done.

Step 2.5: Add a Get Record Element to Find the Related Quotes 

The next step is to use the Get Records element to find the related quotes. 

  1. On Flow Designer, click on the +icon and select the Get Element element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Select the Quote object from the dropdown list.
  4. Select All Conditions Are Met (AND)
  5. Set Filter Conditions
    1. Row 1:
      1. Field: OpportunityId
      2. Operator: Equals
      3. Value: {!$Record.Id}
  6. How Many Records to Store:
    1. select All records
  7. How to Store Record Data:
    1. Choose the option to Automatically store all fields
  8. Click Done.

Step 2.6: Using Decision Element to Check If Quotes Was Found or Not

Now, will use the Decision element to check if the previous Decision element returns quote records or not. 

  1. On Flow Designer, click on the +icon and select the Decision element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Under Outcome Details, enter the Label the API Name will auto-populate.
  4. Condition Requirements to Execute OutcomeAll Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!Get_Quotes}
      2. Operator: Is Null 
      3. Value: {!$GlobalConstant.False}
  5. When to Execute Outcome: If the condition requirements are met.
  6. Click Done.

Step 2.7: Add a Loop Element to Retrieve Records from Record Collection Variable

  1. On Flow Designer, below the Yes node, click on the +icon and select the Loop element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. For Collection Variable select {!Get_Quotes}.
  4. For Specify Direction for Iterating Over Collection select the option First item to last item.
  5. Click Done.

Step 2.8: Adding an Assignment Element to Assign the Quote Id to the Collection Variable 

  1. On Flow Designer, click on the +icon and select the Assignment element. 
  2. Enter a name in the Label the API Name will auto-populate.
  3. Set Variable Values
    1. Row 1:
      1. Field: {!varT_Ids}
      2. Operator: Add
      3. Value: {!Loop_through.Id}
  4. Click Done.

Step 2.9: Add Action – Call an Apex Class

  1. On Flow Designer, click on the +icon and select the Action element.
  2. Search and select the DeleteOpenTasks apex class from the dropdown menu
  3. Enter a name in the Label field; the API Name will auto-populate.
  4. Set Input Values:
    1. Field: Record Ids
    2. Value: {!varT_Ids}
  5. Click Done.

In the end, Arda’s
 
Flow will look like the following screenshot:

Once everything looks good, perform the steps below: 

  1. Click Save.
  2. EnterFlow LabeltheAPI Name will auto-populate.
  3. Click Show Advanced.
  4. API Version for Running the Flow: 55
  5. Interview Label: Query Records by List of Ids in Flow {!$Flow.CurrentDateTime}
  6. Click Save

Almost there! Once everything looks good, click the Activate button.  

Proof of Concept

Now onwards, if a business user updates the Opportunity Stage to Closed Lost, Record-Trigger will automatically delete all open tasks from  Opportunity and related quotes. 

  1. Currently, there are two Quotes attached to the Opportunity West Mountain Sign. Each of them has one open task, as shown in the following screenshot:
  2. Now, we update the Opportunity Stage to Closed Lost.
  3. Navigate back to the Opportunity and Quote activity-related list available on the detailed page.

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!
Preferred Timing(required)

6 thoughts on “Query Records by List of Ids in Flow

  1. Hi Rakesh,

    I have a requirement detailed in below link, I would like to achieve this by Salesforce flow but am not getting any idea.
    Please could you explain me in steps how can I deal with it.

    Objects :
    Opportunity
    CW Products
    CW Product Groups
    Opportunity Product Association
    Opp Product Group Assoc

    Relationship
    Opp Product Group Assoc (MDR to CW Product Groups)
    Opp Product Group Assoc (MDR to Opportunity)
    Opportunity Product Association (MDR to CW Products) junction obj
    Opportunity Product Association (MDR to Opportunity) junction obj

    When I add/create New Opp Product Group Assoc record – flow need to trigger and get all the products from the selected CW Product Group and create Opportunity Product Association record for each product from the group and associate with Opportunity.

    https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000HjZ63SAF

    Many Thanks
    Prema

    1. Rakesh Gupta – Mumbai – 9x Salesforce MVP | Senior Solution Architect | 8x Author | 5x Dreamforce Speaker | Salesforce Coach | Co-host of AutomationHour.com and AppXchangeHour.Com

      No

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Automation Champion

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%