Getting Started with Process Builder – Part 19 (Automatically Add New User to Public Group or Queue)

Getting Started with Process Builder – Part 19 (Automatically Add New User to Public Group or Queue)

Last Updated on November 30, 2020 by Rakesh Gupta

Big Idea or Enduring Question:

How do you automatically add new users to a Public Group or Queue? 

Objectives:

This blog post will help us to understand the following

  • Create automation associated with creating a new user
  • Automatically add a user to a specified Public Group or Queue
  • How to use flows action in Process Builder
  • What is MIXED DML error and how to avoid it

Business Use case

Warren Mason is working as a System administrator at Universal Containers (UC). His organization wants to develop an automation to auto add new users to Public Group Universal Container Users.

Automation Champion Approach (I-do):

In this article Getting Started with Process Builder – Part 24 (Automatically Add User to a Chatter Group), We had discussed a way to auto add new users to the Chatter group using clicks, not code. We also discussed a way to avoid MIXED DML error.

Now let’s take it a step further and discuss a way to auto add new users to Queue or Public Group There are few solutions possible for the above business scenario. We will use Flow and Process Builder to solve the above business requirement.

Before proceeding ahead, you have to understand Group and GroupMember objects. 

  1. Group: – This object represents the Queue and Public Group. They can contain individual users, other groups, the users in a particular role. By using the Type field, you can distinguish between Queue and Public Group. For Public Group (Use Type = Regular) and for Queue (Use Type = Queue).
  2. GroupMember: – Represents a User or Group that is a member of a public group.

Before discussing the solution, 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 5 steps to solve Warren’s business requirement using Flow and Process Builder. We must: 

  1. Create a public group
  2. Find the public group Id
  3. Creating a custom label to store public group Id
  4. Lightning Flow Steps:
    1. Define flow properties for auto-launched flow
    2. Add a text variable to store user-id
    3. Add a create records element to add users to the public group
  5. Process Builder Steps:
    1. Define process properties
    2. Define evaluation criteria
    3. Define process criteria
    4. Set time for actions to execute
    5. Add action – flows 

Step 1: Create a Public Group

  1. Click Setup.
  2. In the Quick Find box, type Public Group.
  3. Click on the Public Group button.
  4. Clicks on the New button
  5. Label the Group then press the tab key, the Group Name will auto-populate. 
  6. Click Save.

Step 2: Find the Public Group Id for UC Internal Announcements 

The next step is to find out the Id of the public group Universal Containers Users. One possible workaround is to use the Developer Console. 

  1. Click Setup | Developer Console 
  2. In the Query Editor, run the following SOQL query 
    1. Select id, name from group where (name=’Universal Container Users’ and Type = ‘Regular’)
  3. Make sure to copy the Id. 

Step 3: Creating a Custom Label to Store Public Group Id

  1. Click Setup.
  2. In the Quick Find box, type Custom Labels.
  3. Select Custom Label then click on the New Custom Label button.
  4. Enter Short Description the Name will auto-populate. 
  5. Now enter the Universal Containers Users public group id in the Value.
  6. Click Save.

Step 4.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 4.2: Lightning Flow – Add  a Text Variable to Store User Id

  1. Under Toolbox, select Manager, then click New Resource to pass the store User Id.
  2. Input the following information: 
    1. Resource Type: Variable
    2. API Name: varTUserId
    3. Data Type: Text
    4. Default Value: {!$GlobalConstant.EmptyString}
    5. Check Available for Input
    6. Check Available for Output
  3. Click Done.

Step 4.3: Lightning Flow – Create Records – Add users to the Public Group

To add a new user to Public Group drag and drop a Record Create onto the flow designer and map the fields according to the below details. 

  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: Group Member
    5. Set Field Values for the Group Member
    6. Row 1:
      1. Field: GroupId
      2. Value: {!$Label.UC_Public_Group_Id}
    7. Click Add Row
    8. Row 2:
      1. Field: UserorGroupId
      2. Value: {!varTUserId}
  3. Click Done.

In the end, Warren’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: Automatically Add New User to Public Group {!$Flow.CurrentDateTime}
  6. Click Save

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

Our next task is to create a Process on the User object to launch a Flow, only when a user is created.

Step 5.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.

Step 5.2: Define Evaluation Criteria

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

Step 5.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: User | IsActive
      2. Operator:  equal
      3. Type: Boolean
      4. Value: True
  5. Select All of the conditions are met (AND)
  6. Click Save.

Step 5.4: set time for actions to execute

DML operations on certain sObjects referred to as setup objects (such as a User Object), along with, on the non-setup object (such as Accounts), in the same transaction, then, a Mixed DML error is thrown at a User.

The Mixed DML error is generated – when a User performs DML actions on Setup and non-Setup Objects in the same transaction -because some sObjects affect the user’s access to records in the org. Therefore, a user must insert or update these types of sObjects in different transactions to prevent operations from happening with incorrect access-level permissions. For example, a user cannot update an account, and a user role, in a single transaction. However, deleting a DML operation has no restrictions (reference Salesforce help).

We are adding time delays to avoid MIXED DML Errors.

  1. Below Scheduled Actions, click Set Schedule.
  2. Set Field Values:
    1. Row 1:
      1. 0
      2. Hours
      3. After
      4. CreatedDate
  3. Click Save.

Step 5.5: Add Action – Flows

  1. Below 0 Hours After CreatedDate Scheduled Actions, click Add Action.
  2. For Action Type, select Flows
  3. Name the action.
  4. Select Flow – Automatically Add New User to Public Group.
    1. Set Flow variables: 
      1. Row 1
        1. Field: varTUserId
        2. Type: Field Reference
        3. Value: User | Id
  5. Click Save.

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

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

Proof of Concept

Now onwards, when a user account is created, Process Builder will automatically trigger and launch the Flow. It will then add the user to a public group (Universal Container Users) post after a few minutes. Let’s test it out.

  1. Now create a new user via the UI
    Create new user
  2. Once the user is created, they are automatically added to the public group Universal Container Users within a few seconds, as shown in the following screenshot:

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? 

Let me know by Tweeting me at @automationchamp, or find me on LinkedIn.

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

12 thoughts on “Getting Started with Process Builder – Part 19 (Automatically Add New User to Public Group or Queue)

  1. Storing hardcoded id of public group (VarT_PublicGroupId ) is not good practice.Because Id will keep on changing environment to environment.Is there any alternative solution for this situation ?
    Thanks for the reply.

  2. What about the existing Users? I want to add existing users to public group, how can I do that? Do I need to update the user last modified date, so that the process builder gets called or is there any other way?

  3. Rakesh, couldn’t you also have set the default value to checked for the onetime checkbox and eliminate the workflow?

  4. Nice article, But I Wonder Why YOU Use A Time Based workflow to update the onetime checkbox? Does this break it to a separate transaction?

Leave a Reply

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