Explain about setup and non-setup objects in salesforce ?? - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Harsha adminHarsha admin 

Explain about setup and non-setup objects in salesforce ??

Can anyone explain why can't we perform dml operations on setup objects ?? clearly 
Best Answer chosen by Harsha admin
Deepak AnandDeepak Anand
Setup objects are objects that are used to interact with the metadata. One common example is the User object. Take a look at this for more information: Setup Objects (https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/reference_objects_setup.htm). Every other objects like those which are native(Standard Objects) and Custom Objects fall into the category of Non-Setup Objects.

Now it is not that you cannot perform CRUD operations on Setup Object records. You can definitely do DMLs on the Setup Objects but not in the same transaction where you're peforming DMLs on Setup and Non-Setup Objects simultaneously. So in other words in a piece of Apex Code you cannot use INSERT / UPDATE / DELETE commands on one set of Setup Objects and one set of Non-Setup Objects together.

You'll have to forcefully perform the DMLs on the Setup Objects in a seperate transaction. One way to handle this(to put them in a different transaction) is to wrap the code(to process Setup objects) in a different Apex Class and in it's own @future annotated method. This would run the code in a different transaction.

Other options includes spawning Batch Apex etc. all of which has an advantage of = "they run in a different transaction" unit.

All Answers

Deepak AnandDeepak Anand
Setup objects are objects that are used to interact with the metadata. One common example is the User object. Take a look at this for more information: Setup Objects (https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/reference_objects_setup.htm). Every other objects like those which are native(Standard Objects) and Custom Objects fall into the category of Non-Setup Objects.

Now it is not that you cannot perform CRUD operations on Setup Object records. You can definitely do DMLs on the Setup Objects but not in the same transaction where you're peforming DMLs on Setup and Non-Setup Objects simultaneously. So in other words in a piece of Apex Code you cannot use INSERT / UPDATE / DELETE commands on one set of Setup Objects and one set of Non-Setup Objects together.

You'll have to forcefully perform the DMLs on the Setup Objects in a seperate transaction. One way to handle this(to put them in a different transaction) is to wrap the code(to process Setup objects) in a different Apex Class and in it's own @future annotated method. This would run the code in a different transaction.

Other options includes spawning Batch Apex etc. all of which has an advantage of = "they run in a different transaction" unit.
This was selected as the best answer
Harsha adminHarsha admin
Thanks deepak anand for the valuable information..
Ribanshu KeshriRibanshu Keshri
it was really helpful
 
Srikanth DarbhaSrikanth Darbha
Hi all,

I have tried to execute the below code and it was succesful, it was not throwing any exceptions(Mixed_DML exceptions)
trigger userupdate on Testa__c (before insert,before update) {
for(Testa__c a:trigger.new)
{
   a.counting_field__c=1;
 List<Testb__c> b = [select id,Counting_field_test__c from Testb__c where name='1'];
 for(Testb__c i:b)
 {
     i.Counting_field_test__c=78;
     update i;
 }
 List<User> u = [select name,id,Count_of_account__c from user where username='srksforce@srksalesforcecorp.com'];
 for(User un:u)
 {
     un.Count_of_account__c=67;
     update un;
 }
   
}

}

Can you please let me know  "So in other words in a piece of Apex Code you cannot use INSERT / UPDATE / DELETE commands on one set of Setup Objects and one set of Non-Setup Objects together." what does this statement mean ? Is the above code not considered as a separate transaction?
Anand ManhasAnand Manhas
trigger userupdate on Testa__c (before insert,before update) {
for(Testa__c a:trigger.new)
{
   a.counting_field__c=1;
 List<Testb__c> b = [select id,Counting_field_test__c from Testb__c where name='1'];
 for(Testb__c i:b)
 {
     i.Counting_field_test__c=78;
     update i;
     insertUser.addUser();
 }   
}
}
******************************
Now you want to keep your other half of code in other transaction like this:-
public with sharing class insertUser {
@future
    public void addUser(){
        List<User> u = [select name,id,Count_of_account__c 
                from user where 
                 username='srksforce@srksalesforcecorp.com'];
 for(User un:u)
 {
     un.Count_of_account__c=67;
     update un;
  }
 }
}

These are separate transactions.

Let me know if this helps.