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
cdavis@elt.comcdavis@elt.com 

MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non...

I have seen some forum saying to use @future to get around this, but I'm not sure how to accomplish that

Below is my trigger, it is short and sweet (well at least I think so). Please have a look and let me know how to fix this.

Thanks!!!!

trigger Touch_Imp on User (after insert) {
if(trigger.isInsert){
    Set<String> usercontactid = new Set<String>(); 
    for (User u : Trigger.new) {
        usercontactid.add(u.contactid);
    }
    if([SELECT COUNT() FROM implementation__c WHERE implementation__c.implementation_contact__c IN :usercontactid]>0){
        Implementation__c[] updateimp = [SELECT id from Implementation__c WHERE implementation__c.implementation_contact__c IN :usercontactid];
        Database.update(updateimp);
    }
}
}

 

dcarmen66dcarmen66

I'm not sure if the @future annotation will work in this case (Salesforce doesn't let you modify a setup object like User and another object in the same context), but in order to use the @future annotation you will need to pull your trigger logic into a separate class, and put the @future annotation on the method. Because it's a future you would need to pass in the Ids of the record.

 

Trigger:

 

Id[] userIds = new Id[]{};

for (User u : Trigger.new) {

   userIds.add(u.Id);

}

 

if (!userIds.isEmpty()) {

    // new class and method

    UserImplementation.processUsers(userIds);

}

 

 

Class

 

public class UserImplementation {

    @future

   public static void processUsers(Id[] userIds) {

   .....

  }

}

cdavis@elt.comcdavis@elt.com

You will have to excuse my ignorance, but I don't understand classes. It is seperate from my trigger?

 

Also with what you have there aren't you creating the User records after the update to the Imp records?

 

What I'm trying to accomplish is simply when a user record is created it checks to see if user.contactid is associated with any of the implementations (think of them as cases). If it is then it updates those Implementations.

I'm doing this to try and get my triggers on my Implementations to trigger. Does this make sense? Is there maybe an all around better way to accomplish this?

 

Also the user record needs to be created before I update the Implementation record as the trigger are causing new sharing rules to be created on the Implementation based upon the User record.