Sweet Potato

Categories
Blog

Record sharing using Salesforce Flow

Sharing plays a crucial role in salesforce. Ever wonder if you can share the record with users based on the data in a lookup field. Well, your wish has come true – the flow now allows you to do this without writing a single line of code. Let’s look at this in details with an use case,

Let’s look at a Universal Container use case:

John is a system administrator and is working at the Universal Container (UC) and managing the recruiters. The user would like to see and edit only the candidates that are associated to the recruiters. For this, he already have a custom object called Candidate’.Once the ‘Recruiter__c(Lookup to User object) field gets populated, auto-share the Candidate record (Grant edit access) with the Recruiter.

Associated Procedures:

There are 3 steps to solve John’s business requirement using After-save Record-Triggered Flow. We must:

  1. Create a custom object with fields
  2. Create apex sharing reason 
  3. Salesforce flow
    1. Define flow properties for record-triggered flow
    2. Create a record variable (varR_RecuritShare) to store Recurit__Share
    3. Add a decision element to check if ‘Recruiter__cis populated or not 
    4. Add an assignment element to populate record variable (varR_‘RecruiterShare)
    5. Add a create records element  to share the Candidate record with the Recruiter

Step 1: Create Custom Object – Candidate

  1. Click Setup.
  2. In the Object Manager, click Create | Custom Object.
  3. Now create a custom object Candidate and fields as shown in the screenshot below: 
  4. Click Save.

Step 2: Create Apex Sharing Reason

Then we will create an Apex Sharing Reason. Each Apex sharing reason has a Label and a Name. The Label value is displayed in the Reason column when viewing the sharing for a record in the user interface. This allows users and administrators to understand the source of the sharing. 

  1. Click Setup.
  2. In the Object Manager, type Candidate.
  3. Select Apex Sharing Reasons, then click New.
  4. Enter Reason Label and press the Tab key the Reason Name will populate. 
  5. Click Save.

Step 3.1: Salesforce Flow – 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 and click on Next and configure the flow as follows:
    1. How do you want to start building: Freeform
    2. Object: Candidate
    3. Trigger the Flow When: A record is created or updated 
    4. Set Entry Criteria
    5. Condition Requirements: None
  5. Optimize the Flow For: Action and Related Records 
  6. Click Done.

Step 3.2: Salesforce Flow – Create a Record Variable to Store Candaiate__Share 

  1. Under Toolbox, select Manager, then click New Resource to store the Candaidate__Share mapping detail. 
  2. Input the following information:
    1. Resource Type: Variable
    2. API Name: CandidateShare
    3. Data Type: Record
    4. Object: Share: Candidate
    5. Check Available for Input
    6. Check Available for Output
  3. Click Done

Step 3.3: Salesforce Flow – Using Decision Element to Check if Recuriter field Is Populated or Not 

Now we will use the Decision element to check the Candaidte__c from Candidate object to find if it is populated by user or not. 

  1. Under Toolbox, select Element. 
  2. Drag-and-drop Decision element onto the Flow designer. 
  3. Enter a name in the Label field; the API Name will auto-populate.
  4. Under Outcome Details, enter the Label the API Name will auto-populate.
  5. Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!$Record.Recuruiter__c}
      2. Operator: Is Null 
      3. Value: {!$GlobalConstant.False}
    2. Add Field
    3. Row 2:
      1. Resource: {!$Record.Recuruiter__c}
      2. Operator: Is Changed 
      3. Value: {!$GlobalConstant.True
  6. Click Done.

Step 3.4: Adding an Assignment Element to Populate Candidate__Share Record Variable 

The next step is to populate the record variable Candidate__share to share the Candidate record with Candidates.

  1. Under Toolbox, select Assignment. Drag and drop Assignment onto the canvas. 
  2. Input the following information:
    1. Enter Label the API Name will auto-populate.
    2. Row 1:
      1. Field: {!CandidateShare.AccessLevel}
      2. Operator: Equals
      3. Value: Edit
      4. Click Add Row
    3. Row 2:
      1. Field: {!CandidateShare.ParentId}
      2. Operator: Equals
      3. Value: {!$Record.Id}
      4. Click Add Row
    4. Row 3:
      1. Field: {!CandidateShare.UserOrGroupId}
      2. Operator: Equals
      3. Value: {!$Record.Candidate__c}
    5. Click Add Row
    6. Row 4:
      1. Field: {!CandidateShare.RowCause}
      2. Operator: Equals
      3. Value: Shared_via_flow__c
  3. Click Done.

Step 3.5: Adding a Create Records Element to Share the Candidate Record with Recuriter

  1. Under Toolbox, select Element. 
  2. Drag-and-drop the Create Records element onto the Flow designer. 
  3. Enter a name in the Label field- the API Name will auto-populate.
  4. For How Many Records to Create select One.
  5. Map Record Collection: {!CandidateShare}
  6. Click Done.

Finally the flow will look like this in the below screenshot.

Leave a Reply

Your email address will not be published. Required fields are marked *