function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RjSanchezRjSanchez 

Error when executing a Trigger on User insert.

The idea is to Automatically follow all other chatter users when a new user is added.

 

The problem I am having is the trigger works when adding a new user as long as a Role is not specified upon creation.

When a Role is selected during creation the following error occurs.

caused by: System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): EntitySubscription, original object: User: []

 

Below is the trigger I have written:

trigger FollowAllChatterUsers on User (after insert) {
    //Create List to hold all chatterIds. they will need to be added.
    List<User> chatterIdsToAdd = new List<User>([SELECT Id from User WHERE IsActive = true]);
    Id currentUserId;
    
    //Take a count of how many users were added to fire this trigger.
    //If more than 10 then do not automatically follow.
    //The assumption is that if we batch-add more than 10 records we are probably
    //  loading saved data or backup data in which case the entitysubscriptions table
    //  will need to be loaded as well.
    Integer triggerLength = 0;
    for( User usr: Trigger.new ) {
        triggerLength++;
    }
    
    if (triggerLength <= 10) {
        // for each inserted user add all chatter ids except their own.
        for( User usr: Trigger.new ) {
            //Array of subscription records to create
            EntitySubscription[] subsToCreate = new EntitySubscription[]{};
            //Get the id of the current user.
            currentUserId = usr.Id;
            //Loop through list of users to add.
            for( User toAdd: chatterIdsToAdd ) {
                //If the id to add is our own then skip. otherwise cont..
                if ( toAdd.Id != currentUserId ) {
                    //Create the EntitySubscription record.
                    EntitySubscription follow = new EntitySubscription(
                        parentId = toAdd.Id,
                        subscriberId = currentUserId);
                    //Add new EntitySubscription to Array for insertion. 
                    subsToCreate.add(follow);
                }
            }
            //Insert the array.
            insert subsToCreate;
        }
    }
}

 

I read somewhere that within the same transaction a User record and a Role record cannot be update using DML.

I'm not sure if that the same issue I'm running into or not.

 

If anyone has any insight I would welcome your suggestions.

 

Thank You,

    Ramon

Best Answer chosen by Admin (Salesforce Developers) 
MandyKoolMandyKool

Hi Ramon,

 

As explained in the error message you cannot  perform "DML operation on setup object is not permitted after you have updated a non-setup object ".

 

You can resolve this by placing your business logic in a method with @future annotation. The only thing is that it will be an asynchronous call.

 

Thanks,

Mandar.

All Answers

MandyKoolMandyKool

Hi Ramon,

 

As explained in the error message you cannot  perform "DML operation on setup object is not permitted after you have updated a non-setup object ".

 

You can resolve this by placing your business logic in a method with @future annotation. The only thing is that it will be an asynchronous call.

 

Thanks,

Mandar.

This was selected as the best answer
RjSanchezRjSanchez

Thank you for your reply Kulkarni. I will look into calling @future to see if that is an acceptable solution for us.