As of now Spring '15 release, when create a new user in Salesforce, we cannot auto add user to Chatter Groups, except write Apex Trigger code in User object (see
Bob Buzzard's blog), or get paid app from AppExchange
ChattoMate.
But, with new Visual Flow + Process Builder, this has been made possible without writing code or using paid app.
Use Case: business request to add all Salesforce/Chatter users to a Chatter Group called 'All Users'. There is also an existing process to auto deactivate user if user last login is more than 2 months. By default, deactivate user will be remove user from the Chatter Group. When user request to be re-activate, user should be added back to the Chatter Group. Manual add user to the Group is not ideal as admins prone to forget, we need to look for solution to automate it.
Here is the simple logic to use Visual Flow and Process Builder:
1. Create Process Builder
- When user created or edited, call Visual Flow below
2. Create Visual Flow
- Check is user has been member of the Chatter Group
- If not found, add user to the Chatter Group
But, issue arise as Salesforce do not allow to have Setup change and a Data change at the same. In that case,
creating a user is Setup changed, and
adding user to Chatter Group Member is Data changed. This causes the Mixed DML error. here is the full error message:
UPSERT FAILED --- ERRORS : (MIXED_DML_OPERATION) DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): CollaborationGroupMember, original object: User ---
Workaround: change the transaction of adding user to Chatter group as
future change. To make it simple, we'll create a new custom Checkbox field in User object to flag that particular user need to be added to Chatter Group.
Here is the updated logic:
1. Create a Workflow with Time-Dependent Action
- When user created or edited, and the checkbox is false, perform "future" Field Update action to update the custom field to true
2. Create Process Builder
- When the flag is true, call Visual Flow below
3. Create Visual Flow
- Lookup if user is member of the Chatter Group
- If not found, add user to the Chatter Group
Create Visual Flow
1. Navigate to
Setup | Create | Workflow & Approvals | Flows
2. Click
New Flow button
3. Drag
Record Lookup element onto the canvas
4. Enter Record Lookup Name =
Check User Exist in Chatter Group
5. Select
CollaborationGroupMember as look up object
- CollaborationGroupId = the Chatter Group Id
- MemberId =
create new Variable, name it
UserId
6. Assign the Id to by
create a new variable
7. Name the variable as
GroupMemberId
8. This is what we have for Lookup
9. Click OK button to continue
10. Drag Decision element onto the canvas
11. Enter Decision Name = Lookup Result
12. Enter outcome name = Not Found with condition as below:
13. Click OK button to continue
14. Connect Record Lookup element with Decision element
15. Drag Record Create element onto the canvas
16. Enter Decision Name = Create Membership
17. Select object = CollaborationGroupMember
- CollaborationGroupId = the Chatter Group Id
- CollaborationRole = select PICKLIST VALUES
Standard
[
Admin or Standard]
- MemberId = select variable
UserId
- NotificationFrequency = select PICKLIST VALUES
D
[
D = Daily Digest; P = Each Post; W = Weekly Digest; N = Never]
18. Just leave record Id created, as we do not need it. Click
OK button to continue
19. Connect
Decision element with
Record Create element
20. It will prompt you for Decision Routing, select
Not Found (this is outcome name we set in step 12 above)
21. Hover mouse over Record Lookup and click the green arrow icon to set it as Start element.
22. This is the complete Flow
23. Click
Save button, enter name =
Add User to Chatter Group; select type =
Autolaunched Flow, click
OK to continue
24. Click Close button to back to list of Flows, then click
Activate link, otherwise the flow will not visible in Process Builder.
Create Workflow with Time-Dependent Action
1. Add a custom object Checkbox in User object = Add to Chatter group, with Default Value = unchecked.
2. Create a workflow rule as below:
3. Create Field Update action to update custom object Checkbox "Add to Chatter group" to true. It is important to
select "Re-evaluate Workflow Rules after Field Change", otherwise later on field update here will not trigger Process Builder when this flag set to true.
4. Back to the workflow and add Time Trigger action, set it to
0 hours after Rule trigger date. Usually the trigger will kick-in within 15 minutes as it will be put the actions to queue, you can monitor from
Setup | Time-Based Workflow, click
Search button.
5. Add created field update action (step 3) as Time-Dependent Workflow Actions in this workflow.
6.
Activate the workflow.
Create Process Builder
1. Navigate to
Setup | Create | Workflow & Approvals | Process Builder
2. Click new Button to create new Process Builder
3. Enter name =
Auto Add User to Chatter Group All
4. Select Object =
User, and select Start the process =
when a record is created or edited .Click Save button to continue.
5. Add Criteria, see following setting and click Save to continue
6. In Immediate Actions, call Flow created earlier
7. Click Activate button to activate the Process