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
naga kiran 2naga kiran 2 

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

I have an error  while firing a trigger i need create a user record from custom object below is the code pls resolve it ASAP...

trigger createportaluser on Compliance_Diligence_Association__c (after insert) {

list<user> userList = new list<user>();
    for(Compliance_Diligence_Association__c objcustom : [SELECT GLD_Submit_Case__c ,GLD_Business_Contact__r.Id,GLD_Business_Contact__r.FirstName,GLD_Business_Contact__r.LastName,GLD_Business_Contact__r.Email FROM Compliance_Diligence_Association__c where id in: trigger.new]){
          if( objcustom.GLD_Submit_Case__c == true){
   
    User u = new User();
             u.ContactId=objcustom.GLD_Business_Contact__r.Id;  
         u.username=objcustom.GLD_Business_Contact__r.Email;
         u.firstname=objcustom.GLD_Business_Contact__r.FirstName;
         u.lastname=objcustom.GLD_Business_Contact__r.LastName;
         u.email=objcustom.GLD_Business_Contact__r.Email;
         u.TimeZoneSidKey='America/Los_Angeles';
         u.LocaleSidKey = 'en_US';
         u.EmailEncodingKey='UTF-8';
         u.LanguageLocaleKey = 'en_US';
         u.alias = string.valueof(objcustom.GLD_Business_Contact__r.FirstName.substring(0,1) + objcustom.GLD_Business_Contact__r.LastName.substring(0,1));
         u.profileid = '00e61000000ndg3'; 
         u.CommunityNickname=string.valueof(objcustom.GLD_Business_Contact__r.FirstName.substring(0,1) + objcustom.GLD_Business_Contact__r.LastName.substring(0,1));

  
        userList.add(u);

             
           }
        }
        insert(userList);
        }

Thanks,
Naga
D-CoderD-Coder
You should use @future class to avoid this error , Try this :

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects.htm?search_text=mixed%20dml

 
naga kiran 2naga kiran 2
Hi nilesh,

I am new to apex coding can you please seggregate above trigger to be future method...so that i will learn...

Thanks,
Naga