Query for a record in its related object by using flow. [Salesforce]
Tags: tulippattra, flow, salesforceflow, querycrossobject
Published: 2021-05-04 13:19:48

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e73bwlgfgmma32thj88o.jpg)

Flow:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opt9982x4p7bnkdztg4s.jpg)

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 **
8. Under "Toolbox" > select "Element".
9. Drag a "Get Record" element into canvas (working area).
10. Enter label "Auto populate"
11. Select the Object: Request Product Discount.
12. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qlz0h25saacy79ltun3.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xylboy3ekgnfcegjv6t4.png)


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

13. Drag the decision element from the toolbox.
14. Label it as "Found in project"
15. Input the conditions: 
    - Condition Requirements to Execute Outcome: All conditions are met.
    - Resource: {!Requested_Product_c.id} 
    - Operator: not null
    - Value: False
16. When to Execute Outcome: If the condition requirements are met
17. Change the default outcome label to "Not found in project"

Almost there...
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqqa75oy1q100k4oxaen.png)

** Assignment Element **
There are 2 elements we need to create.
The first one is when the record does meet the criteria.
18. Drag the "assignment" element
19. Set Variable Values:
    - Variable: OpportunityLine.Approved_in_Project__c
    - Operator: Equal
    - Value: TRUE
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9j4snv8dpvo1el499hup.png)
And the second one is when the record does not meet the criteria.
20. Drag the "assignment" element
21. Set Variable Values:
    - Variable: OpportunityLine.Approved_in_Project__c
    - Operator: Equal
    - Value: False
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eo7nvmzi5dqtau03rysy.png)

22. Connect it all together like the picture below.
23. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9e285pqm5sd95xe28nmk.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovq6qxbn366haipkxkr1.png)


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5atl2cwzifq1gsrklg38.png)