salesforce-user-automation
Skill1 starsChanged 3 months agoMITView on GitHub
Automation on the Salesforce User object. TRIGGER when: building a flow, trigger, or Queueable that touches User plus any non-setup object, MIXED_DML_OPERATION, CANNOT_EXECUTE_FLOW_TRIGGER on user activation, Chatter group membership DML, a User field that deploys but reads as nonexistent, or filtering for real staff accounts. Not for creating or de-provisioning users.
In this repository11 other files it gives its agents.
The file
Files
salesforce-user-automation
User is a setup object and most things you want to touch alongside it are not, so automation on
User carries traps that a clean deploy will not catch. Every item here deploys green, passes tests,
and fails somewhere else.
Point-in-time: these were observed on Salesforce orgs during 2026. Verify against your own org before asserting any of it as current fact.
Scope
| In scope | Mixed DML on User paths (flow and Apex), async-path and chained-Queueable patterns, the testing traps, FLS-less User fields, Chatter group membership DML and queries, filtering for real staff accounts |
| Out of scope | Creating, reactivating, or de-provisioning users; access errors on a working user; permission set authoring |
The traps
- Put the non-setup DML on an ASYNCHRONOUS path (
<pathType>AsyncAfterCommit</pathType>), not the immediate path. Creating aCollaborationGroupMemberin the triggering transaction works on insert but throwsMIXED_DML_OPERATIONon update, and it surfaces asCANNOT_EXECUTE_FLOW_TRIGGER("We can't save this record"), so activating a user fails outright. The insert-path pass is what makes this dangerous: the flow looks correct until someone activates a user. An async path runs after commit in its own transaction, which removes the restriction and also means a failure there can never block a user save. A<label>on an asyncscheduledPathsis rejected at deploy (Label ... cannot be set for ScheduledPath of PathType):name,connector, andpathTypeonly. - An async path is not enough, because two DML types in ONE job collide the same way, and
allOrNone: falsemakes the collision SILENT. A job that reconciles a setup object (sayUserPackageLicense) and a non-setup object (sayCollaborationGroupMember) fails the second DML withMIXED_DML_OPERATION, and underDatabase.insert(records, false)that arrives as a failedSaveResult, never a thrown exception. So nothing throws, aFinalizernever retries, andAsyncApexJobreadsStatus=Completed, NumberOfErrors=0while the work simply did not happen. One such defect shipped with 13 green tests, a clean production check-only validation, and 95% coverage. Put the setup DML in its OWN chained Queueable, and treat any reconcile job touching both a setup and a non-setup object as suspect until you have watched it run. - A test that dodges the real-world precondition stays green forever. The tests above passed
because they ran against a Chatter group name that did not exist, so the reconcile returned early
and the two DML types never met. The one combination that occurs in every real org was the one
never tested, and the test class documented the dodge as though it were a design choice. Write the
regression test with the precondition present. Apex forbids chaining a queueable from a queueable
inside a test, so assert the chain by calling the reconcile directly and checking
Limits.getQueueableJobs(), rather than by enqueuing it. - Testing this in anonymous Apex will lie to you if you update the RUNNING user. DML on your own
Userrecord is exempt from the mixed-DML restriction, soupdate [SELECT ... WHERE Id = :UserInfo.getUserId()]followed by aCollaborationGroupMemberinsert succeeds, and reads as proof that mixed DML is fine here. Test against a different user. The reverse order trips too (CollaborationGroupthenUserin one transaction), so split cleanup scripts into separate executions. - A custom field on
Userwith NO FLS grant is invisible tosf sobject describeAND uncompilable in Apex ("Field does not exist"), even for a System Administrator, whilesf org list metadata --metadata-type CustomFieldstill lists it. So "the deploy succeeded but the field does not exist" means no FLS, not a failed deploy. This is the mirror of theFieldDefinition-is-FLS-filtered trap, and it is worth recognizing quickly because the obvious reading (redeploy the field) never fixes it. CollaborationGroupMember.NotificationFrequencyinherits the member's ownUser.DefaultGroupNotificationFrequencywhen you leave it unset, which is commonlyP(email on every post). An unset insert therefore emails those people on every future post to the group. Set it explicitly toN. The field describe reports a default ofN, which is misleading: the describe default is not what an unset insert gets. A Chatter announcement's "Email all group members" option overrides the setting either way.IsActive = true AND FederationIdentifier != nullis NOT "only humans". An integration or vendor service account provisioned through SSO holds a Federation Identifier too, and one of those inside a "notify all staff" automation is how a service account ends up in a Chatter group or an all-staff email. No single field discriminates reliably, so measure your own org before picking a marker: count how many active users have each ofUserRoleId,EmployeeNumber,ManagerId, andDepartmentpopulated, and choose whichever your provisioning process actually fills every time. In one org measured for this,UserRoleId != nullwas the only marker that covered every staff account while excluding the sole service account holding a Federation Identifier;EmployeeNumberwas populated for well under 1% of active users, and several real people shared the System Administrator profile, so neither of those worked. Where a role is the marker, clearing a role is also how you take a service account out of such automation by hand. The residual risk runs one way: a staff user provisioned without the marker is silently skipped, which is itself a provisioning defect worth catching rather than working around.
Counting Chatter membership through the Member relationship silently drops rows
WHERE Member.LastName LIKE 'x%' returned three of a group's four real rows, and
SELECT Member.Name ... under-returned against COUNT(Id) on the same group. The misses are
inactive users, whom the relationship traversal filters out.
It fails silently and in the direction that looks like success: a deactivated member reads as
"already removed", so the query will confirm a removal that never happened. Query MemberId alone
and join to User locally. The same rule applies to any membership existence check whose answer
you are about to act on.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

