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
Krishnakanth Pamu 1Krishnakanth Pamu 1 

mixed dml error generate

Just i want to generate DML Exception error using below code but it is executing successfully even though i am doing UPDATING USER OBJECT and CREATING AND CREATIGN CUSTOM OBJECT in the SAME APEX CODE..

Below is the code.. Please let me know if i miss to apply anything..


public class TestMixedDmlError {
    
    public static void function1(){
        Indeed_com_AppForm__c i;
        User u = [select id, name, CompanyName from user where name='UserName1'];       
        if(u.CompanyName == null){
            u.CompanyName = u.Name + ' ' + 'Company';
            i = new Indeed_com_AppForm__c();
            i.Name = 'Created from TestMixedDmlError Apex Class';
            insert i;
            update u;
        }
        system.debug(u.CompanyName);
        system.debug(i.name);
          
    }
    
 

}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Krishnakanth,

I found the below article, for example, can you check this once:

>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects.htm

I hope this helps and in case if this came in handy in addressing the issue can you please choose this as best answer so that the question is closed and it would in turn help in keeping our community clean, apart from this it would also help others in case if they have a similar issue in the future.

Regards,
Anutej