I have a complex Apex trigger that calls two separate queueable jobs - one job inserts Permission Set Assignments, and another job creates and deletes ContactShare records. I am positive that neither job can invoke the other via trigger actions.
When I run the unit test, I get a mixed DML error from the second job because of the Permission Set Assignment creation in the first job.
Question: If queued jobs are running within the same startTest/stopTest
block, can they cause Mixed DML issues with each other? Is there a workaround for it?
The two jobs are initiated as part of a single Apex transaction (inserting a user). I have the user insertion wrapped in a System.runAs()
in the test, and it's in a startTest/stopTest block.
I can see from the debug log that when the first job is executed:
14:39:20.596 (6305771684)|CODE_UNIT_STARTED|[EXTERNAL]|01pK0000000HEBm|DESK_UserDomain
14:39:20.596 (6305842037)|SYSTEM_METHOD_ENTRY|[14]|QueueableContextImpl.QueueableContextImpl()
14:39:20.596 (6305854657)|SYSTEM_METHOD_EXIT|[14]|QueueableContextImpl
14:39:20.596 (6308757099)|DML_BEGIN|[396]|Op:Insert|Type:PermissionSetAssignment|Rows:1
I see the second job starting as well, but there's no reference to QueueableContextImpl.QueueableContextImpl. Does that occur only once and apply to all queuable jobs spawned within the startTest/stopTest
block?
14:39:20.596 (6494302250)|CODE_UNIT_STARTED|[EXTERNAL]|01pK0000000HAZp|HL_ClientDataSharing
14:39:20.596 (6496669468)|USER_DEBUG|[464]|### Starting Queued Execution for HL_ClientDataSharing.queueClientDataSharing()