Automation Champion

Automating Salesforce One Click at a Time
Automation Champion
  • Home
  • About Me
    • Testimonials
    • Resources
  • Process Builder
  • Salesforce Flow
  • Pardot
  • Apex
  • Training Details
    • Salesforce Administrator
    • Salesforce Advanced Administrator
    • Platform App Builder
    • Lightning Flow
    • Pardot
    • Sales Cloud
    • Service Cloud
    • Community Cloud
    • Hands-on Excercises
  • My Books
  • Contact Me
  • Tag: Sharing a Record Using Process Builder

    • Getting Started with Process Builder – Part 16 (Creating Custom Record Sharing Logic)

      Posted at 12:12 AM by Rakesh Gupta, on April 7, 2015

      Big Idea or Enduring Question:

      • Sharing records automatically without sharing rules or role hierarchy
      • Using Apex Managed Sharing declaratively

      Objectives:

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

      • Create declarative automation to automatically share a record
      • Share a record without sharing rules or role hierarchy
      • Start a Flow from a Process Builder

      Business Use Case

      Brenda David is working as a System administrator at Universal Containers (UC). Currently, she is working on a project that is implementing audit management for Assets. For this she has created a custom object Audit (OWD: – private) and few custom fields as shown in the following screenshot: Part 16.1 The business requirement: 

      • as soon as Auditor__c (Lookup to User object) field gets populated, auto-share the Audit record (Grant edit access) with the auditor.

      Automation Champion Approach (I-do):

      Salesforce allows you to control data access at many different levels (object-level, record-level, and field-level). We will focus on methods for controlling access to data at the record level. 

      In most scenarios, you can use out-of-the-box sharing settings to grant record access, but there are few use cases where you have to write an Apex Managed Sharing rule to share the records with users or groups. 

      Apex Managed Sharing allows you to use Apex code to build sophisticated and dynamic sharing settings that aren’t otherwise possible. In this article, we will show how to use Lightning Flow and Process Builder to solve these requirements instead of using Apex.

      Before proceeding, make sure you understand the sharing table in Salesforce. All objects that have a default sharing setting of either Private or Public Read Only. These objects also have a related Share object that is similar to an access control list (ACL) found on other platforms. 

      All Share objects for custom objects are named as CustomObject__Share, where CustomObject__c is the name of the related custom object. A shared object includes records supporting all three types of sharing (Force.com managed sharing, User managed sharing, and Apex managed sharing). 

      The Share object contains the following fields:

      • ParentId: – The Id of the record being shared. This field cannot be updated.
      • UserOrGroupId: – The Id of the User to whom you are granting access.
      • AccessLevel: – The level of access that the specified User or Group has been granted.
      • RowCause (aka Sharing Reasons): – The reason why the user or group is being granted access.

      Before discussing it, let me show you a diagram of a Process Flow at a high level. Please spend a few minutes to go through the following Flow diagram and understand it.

      Let’s begin building this automation process.

      Guided Practice (We-do):

      There are 4 steps to solve Brenda’s business requirement using Flow and Process Builder. We must: 

      1. Create a custom object with fields
      2. Create apex sharing reason 
      3. Lightning Flow Steps:
        1. Define flow properties for auto-launched flow
        2. Add a record variable to store an audit record data
        3. Add a create records element  – share the record with the auditor 
      4. Process Builder Steps:
        1. Define process properties
        2. Define evaluation criteria
        3. Define process criteria
        4. Add action – flows 

      Step 1: Create a Custom Object Audit and Fields 

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

      Part 16.1

      Step 2: Create Apex Sharing Reason

      First, 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 Audit.
      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: Lightning 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 Autolaunched Flow (No Trigger) option and click on Next and configure the flow as follows: 
        1. How do you want to start building: Freeform
      5. Click Done.

      Step 3.2: Lightning Flow – Add Record Variable to Store Audit Record

      1. Under Toolbox, select Manager, then click New Resource to pass the Audit Id.
      2. Input the following information: 
        1. Resource Type: Variable
        2. API Name: varRAudit
        3. Data Type: Record
        4. Object: Audit
        5. Check Available for Input
        6. Check Available for Output
      3. Click Done.

       

      Step 3.3: Lightning Flow – Create Records – Share the Record with Auditor 

      The next step is to create a record on the Audit_Share object to share the record with the auditor at the run time.

      1. Under Toolbox, select Elements. Drag and drop Create Records onto the canvas. 
      2. Input the following information:
        1. Enter Label the API Name will auto-populate.
        2. How Many Records to Create: One
        3. How to Set the Record Fields: Use separate resources, and literal values
        4. Object: Share: Audit
        5. Set Field Values for the Share: Audit
        6. Row 1:
          1. Field: AccessLevel
          2. Value: Edit
        7. Click Add Row
        8. Row 2:
          1. Field: ParentId
          2. Value: {!varRAudit.Id}
        9. Click Add Row
        10. Row 3:
          1. Field: UserOrGroupId
          2. Value: {!varRAudit.Auditor__c}
        11. Click Add Row
        12. Row 4:
          1. Field: RowCause
          2. Value: Share_record_with_Auditor__c
      3. Click Done.

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

      Once everything looks good, perform the steps below: 

      1. Click Save.
      2. Enter Flow Label the API Name will auto-populate.
      3. Click Show Advanced.
      4. API Version for Running the Flow: 50
      5. Interview Label: Share record with Auditor {!$Flow.CurrentDateTime}
      6. Click Save. 

      Almost there! Once everything looks good, click the Activate button. Our next task is to create a Process Builder on the Audit object to start a Flow. 

      Step 4.1: Define Process Properties

      1. Click Setup.
      2. In the Quick Find box, type Process Builder.
      3. Select Process Builder, then click New.
      4. Name the Process and click the Tab button. The API Name will populate. 
      5. As a best practice, always input a description. 
      6. The process starts when A record changes.
      7. Click Save.

      Part 16.5

      Step 4.2: Define Evaluation Criteria

      1. Click on the Add Object node to begin selecting the evaluation criteria.
      2. Select the Audit object from the dropdown list.
      3. Start the process when a record is created or edited.
      4. Click Save.

      Part 16.6

      Step 4.3: Define Process Criteria

      1. Click the Add Criteria node to begin defining the process criteria.
      2. Name the criteria.
      3. The criteria should execute actions when the conditions are met.
      4. Set Conditions
        1. Row 1
          1. Field: Audit__c | Auditor__c
          2. Operator: Is null
          3. Type: Boolean
          4. Value: False
      5. Select All of the conditions are met (AND). 
      6. Click Advanced. 
      7. Select Yes to execute the actions only when specified changes are made to the record.
      8. Click Save.

      Part 16.7

      The reason why we would select the Yes checkbox for the question — Do you want to execute the actions only when specified changes are made to the record? — is to allow the Process Builder 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 4.4: Add Action – Flows

      1. Below Immediate Actions, click Add Action.
      2. For Action Type, select Flows. 
      3. Name the action.
      4. Select the flow we just created – Share record with Auditor.
      5. Set Flow Variables: 
        1. Row 1:
          1. Flow Variable: varRAudit
          2. Type: Field Reference
          3. Value: Select the Audit__c record that started your process
      6. Click Save.

      In the end, Brenda’s Process will look like the following screenshot:

      Part 16.8

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

      Proof of Concept

      Now onwards, When a business user creates an audit record with an Auditor, then Process Builder will automatically trigger and launch the Flow which will share the audit record with the auditor. You will have to do some of this testing in Classic.

      1. Currently, the audit for Dell PowerEdge Tower Servers doesn’t have an auditor, as shown in the following screenshot:
      2. Now update the audit record with the valid auditor:
      3.  To verify the outcome, navigate to Classic click on the sharing button available on the record detail page, and check out the sharing detail as shown in the following screenshot:

      Final Outcome

      Independent Practice (You-do):

      • In a sandbox or dev/Trailhead org, create a quick custom object with a look-up to the Opportunity record
        • Include a User look-up
      • Create a sharing rule as outlined above

      Formative Assessment:

      Identify a custom object in your org that should be shared based on the settings in a related object and create an autolaunched flow that will share the records without actual manual sharing

      Post a picture of the sharing on Twitter @automationchamp, #AutomatedSharingwithFlow

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

      Posted in Process Builder, Salesforce Flow, Spring'15 Release | 16 Comments | Tagged Add Criteria Proces builder, Advanced flow, Apex based record sharing in Salesforce, Apex Managed Sharing, Apex managed sharing setting, Apex managed sharing settings in Salesforce, Apex Share, Apex Sharing Reasons, call a flow thorugh PB, Creating Custom Record Sharing Logic, Enable Process Builder, flow, Flow and PB examples, Flow and Prcoess Builder examples, FLow and Process builder example, Flow based record sharing in Salesforce, Getting started with Process Builder, Grant Read/Write/Delete access via Apex Managed Sharing, Grant Read/Write/Delete access via Apex Managed Sharing using FLow, How to activate Process builder, how to use Process builder, Launch flow from PB, Lightning App, Lightning Process builder, PB, Prcoess builder and Chatter use case, Prcoess Builder based record sharing in Salesforce, Process Builder, Process Builder and Gamification, Process Builder examples, Process Builder Salesforce, Process Builder update a record action, Process builder use case, Programmatically Creating Sharing Rules with Apex, RowCause, Sharing a Record Using Apex, Sharing a Record Using Flow, Sharing a Record Using Flow and Process Builder, Sharing a Record Using Process Builder, sharing settings, sharing settings in Salesforce, sharing table in Salesforce, Spring15 process builder, Spring15 Process builder enhancement, Understanding Apex Managed Sharing, Understanding Flow Managed Sharing, Understanding Process Builder Managed Sharing, Update Records, Use Case for Apex Managed Sharing, Use Case for Flow Managed Sharing, use click to create Sharing Rules with Apex, Using Apex Managed Sharing, What is Apex Managed Sharing in Salesforce
    • Search

    • Upcoming Trainings

      Salesforce Administrator Certification
      Salesforce Advanced Administrator Certification
      Salesforce Platform App Builder Certification
      Lightning Flow & Process Builder
      Pardot Specialist & Consultant Certification
      Sales Cloud Consultant Certification
      Service Cloud Consultant Certification
      Community Cloud Consultant Certification
    • Sponsor #1

    • Sponsor #2

    • Sponsor #3

    • Sponsor #4

    • Order Now!

    • Buy My Book

    • Buy My Book

    • Recent Posts

      • Getting Started with Salesforce Flow – Part 54 (Auto-follow a Chatter Question After Reply)
      • Getting Started with Salesforce Flow – Part 53 (Clone a User with their associated Public Group, Queue Membership and Permission Sets)
      • Getting Started with Salesforce Flow – Part 51 (Add a Flow to a Lightning Page)
      • Getting Started with Salesforce Flow – Part 50 (Welcome New Users by Default – Auto Follow them in Chatter!)
      • Top 5 Lightning Component Gems of Salesforce Spring’21 Release!
    • Salesforce ID Converter Build on Lightning Flow

      Converting 15 digit ID to 18 digit Salesforce ID
    • Blog Archives

    • Categories

  • Information

    • About Me
    • Affiliate Disclaimer
    • Contact Me
    • Privacy Policy
  • Top Posts

    • Salesforce Spring’21 Release Quick Summary
    • Learning Flow
    • Learning Process Builder
    • Getting Started with Process Builder - Part 1 (Automatically Create a record)
    • Getting Started with Process Builder – Part 30 (Auto-generate Renewal Opportunity With Line Items)
    • Common Deployment Issues in Salesforce & How to Solve for Them
    • Getting Started with Salesforce Flow – Part 32 (Preschedule Chatter Posts)
    • Getting Started with Salesforce Flow – Part 1 (Understand, Create & Use Variables in Flow!)
    • Get Community Cloud Certification - Demand is High and Supply Low!
    • Top 5 Lightning Component Gems of Salesforce Spring'21 Release!
  • Social Media

    • View Automationchampion’s profile on Facebook
    • View Automationchamp’s profile on Twitter
    • View Rakeshistom’s profile on GitHub