DEV Community

Tulip Pattra B
Tulip Pattra B

Posted on

Query for a record in its related object by using flow. [Salesforce]

Business Use case:

  • Tulip has been working as a Salesforce consultant for a manufacturing company. One day, she was assigned a new requirement to just notify sales users that the product they select under the opportunity line with the discount has been approved to sell for a related project or not.

Object Diagram:
Alt Text

Flow:
Alt Text

The steps of procedures:

First of all, a project object and a requested product object with all required fields must be created before building the flow.

** Entry criteria **

  1. Click Setup > search for "Flow" from the quick find box.
  2. Select Flows then click on the "New Flow".
  3. Select the "Record-Triggered Flow option" and then select "Freeform".
  4. Edit the configure trigger by following: 4.1. Trigger the Flow When: A record is created or Updated 4.2. Run Flow: Before the record is saved
  5. Choose Object: OpportunityLineItem
  6. Select Condition Requirements: All conditions are met (AND) and set the condition

    • Field: Product2Id
    • Operator: Does not equal
    • Value: EmptyString (NotNull)
  7. When to Run the Flow for Updated Records: Every time a record is updated and meets the condition requirements

** Get Record Element **

  1. Under "Toolbox" > select "Element".
  2. Drag a "Get Record" element into canvas (working area).
  3. Enter label "Auto populate"
  4. Select the Object: Request Product Discount.
  5. Filter for the record. **This step is to search for the desired record. Entry the conditions for finding the record.
    • Project_c = OpportunityLine.Opportunity.Project_c
    • Product_c = OpportunityLine.Product_c
    • Req_Discount__c Greater or equal OpportunityLine.Discount
    • Status__c = Approved

Sort Request Discount (Product) Records: Not sort
How Many Records to Store: Only the first record
How to Store Record Data: Automatically store all fields

Done!!.. Kidding.. There are 3 elements to go!!. Don't give up>)

Alt Text
Alt Text

** Decision Element **
Now we will use the Decision element to check the size of the previous step.

  1. Drag the decision element from the toolbox.
  2. Label it as "Found in project"
  3. Input the conditions:
    • Condition Requirements to Execute Outcome: All conditions are met.
    • Resource: {!Requested_Product_c.id}
    • Operator: not null
    • Value: False
  4. When to Execute Outcome: If the condition requirements are met
  5. Change the default outcome label to "Not found in project"

Almost there...
Alt Text

** Assignment Element **
There are 2 elements we need to create.
The first one is when the record does meet the criteria.

  1. Drag the "assignment" element
  2. Set Variable Values:
    • Variable: OpportunityLine.Approved_in_Project__c
    • Operator: Equal
    • Value: TRUE Alt Text And the second one is when the record does not meet the criteria.
  3. Drag the "assignment" element
  4. Set Variable Values:

    • Variable: OpportunityLine.Approved_in_Project__c
    • Operator: Equal
    • Value: False Alt Text
  5. Connect it all together like the picture below.

  6. Click "Save" and "Activate" to finish building the flow. :)))

Finally, don't forget to test after we finish developing.. :)

  • Add the product to the project and set the discount.
    Alt Text

  • Add product to the opportunity line item with the discount.

  • After saving, you will see the "approve in project" checkbox is checked. If you discount greater than 25% that I set before, it will be unchecked.

Alt Text

Alt Text

Top comments (0)