Hi Surendra, There are two possibilities so you are not getting exception 1) I think you are not getting Mixed DML Operation because after insert and before update are two different transactions, Mixed DML operations are not allowed in single transaction (check debug logs if u have CODE_UNIT_STARTED 2 times under event tab) 2) Assuming you are not inserting or updating any other sobject other than user in after insert and before update trigger then there is no question of getting Mixed DML operation exception as you are not combining setup object with non setup objects.
If we perform DML operation on standard/custom object and global objects(User, UserRole, Group, GroupMember, Permission Set, etc...) in same transaction this error will come. To avoid this error, we should perform DML operation on standard/custom object records in a different transaction. In general all the apex classes and apex triggers execute synchronously (execute immediately). if we perform DML operation on standard/custom object records asynchronously (execute in future context), we can avoid MIXED-DML-OPERATION error. To execute logic asynchronously keep the logic in an apex method (in a separate apex class, not in same apex trigger) which is decorated with@future annotation.
see the below example -
public class TriggerUtility {
/*
1. Following future method execute asynchronously (whenever server is free it will execute in future context).
2. We should not declare @future method in Apex Trigger.
3. @future method should be always static.
4. @future method accepts only primitive data types (Integer, String, Boolean, Date, etc...) as parameters and it won't accept
non-primitive data types (sObject,Custom Objects and standard Objects etc.. ) as parameters.
5. @future method should not contain return type. Always it should be void.
6. From an apex trigger we can make only make asynchronous call outs. To make call out we should include "callout = true" beside the future @annotation.
7. We cannot perform synchronous call outs from Apex Trigger.
*/
//Below is the example for the future method -
@future(callout = true)
public static void processAsync(primitive parameters) {
//Logic to insert/update the standard/custom object.
}
}
Thanks! Mark it has correct Answer if t helps you,it may help to others even!
Mixed DML operations inside one transaction aren’t allowed. You can’t perform DML on a setup sObject and another sObject within the same transaction. However, you'll perform one style of DML as a part of the Associate in Nursing asynchronous job and also the alternatives in other asynchronous jobs or within the original transaction.
Please mark it as the best answer if it helps you to fix the issue.
In order to understand what mixed DML error is and how to resolve it with proper example then watch this youtube video, its very helpful!! https://youtu.be/feC1hwElGaM
There are two possibilities so you are not getting exception
1) I think you are not getting Mixed DML Operation because after insert and before update are two different transactions, Mixed DML operations are not allowed in single transaction (check debug logs if u have CODE_UNIT_STARTED 2 times under event tab)
2) Assuming you are not inserting or updating any other sobject other than user in after insert and before update trigger then there is no question of getting Mixed DML operation exception as you are not combining setup object with non setup objects.
Thanks,
Nirdesh
If we perform DML operation on standard/custom object and global objects(User, UserRole, Group, GroupMember, Permission Set, etc...) in same transaction this error will come.
To avoid this error, we should perform DML operation on standard/custom object records in a different transaction.
In general all the apex classes and apex triggers execute synchronously (execute immediately).
if we perform DML operation on standard/custom object records asynchronously (execute in future context), we can avoid MIXED-DML-OPERATION error.
To execute logic asynchronously keep the logic in an apex method (in a separate apex class, not in same apex trigger) which is decorated with@future annotation.
see the below example -
Thanks!
Mark it has correct Answer if t helps you,it may help to others even!
Greetings!
Mixed DML operations inside one transaction aren’t allowed. You can’t perform DML on a setup sObject and another sObject within the same transaction. However, you'll perform one style of DML as a part of the Associate in Nursing asynchronous job and also the alternatives in other asynchronous jobs or within the original transaction.
Please mark it as the best answer if it helps you to fix the issue.
Thank you!
Regards,
Suraj Tripathi
https://youtu.be/feC1hwElGaM