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
aamir arshadaamir arshad 

i am getting the error system.QueryException: List has no rows for assignment to SObject instead of Mix DML Exception

public class Mixed_DML_Example {
  
    public  void callMe(){
           Profile p = [SELECT Id FROM Profile WHERE Name='Test'];
        UserRole r = [SELECT Id FROM UserRole WHERE Name='Manager'];
        // Create new user with a non-null user role ID 
        User u = new User(alias = 'welll', email='salesforcebatch111@gmail.com', 
            emailencodingkey='UTF-8', lastname='wilson11', 
            languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
            timezonesidkey='America/Los_Angeles', 
            username='wilsonDev@221Test.com');
        insert u;
            
            
            Account acc= new Account();
             acc.Name='Testing';
             acc.Phone='12345';
            acc.Rating='Hot';
            insert acc;
            
    }
}
Suraj Tripathi 47Suraj Tripathi 47
Hi Aamir,
Greetings!

In your query,
Profile p = [SELECT Id FROM Profile WHERE Name='Test'];
UserRole r = [SELECT Id FROM UserRole WHERE Name='Manager'];
One of them is returning zero rows because there is no Profile named Test or there is no UserRole named Manager.

So, if you want to solve this use List of SObject.
Like-
List<Profile> p = [SELECT Id FROM Profile WHERE Name='Test'];
List<UserRole> r = [SELECT Id FROM UserRole WHERE Name='Manager'];

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi
aamir arshadaamir arshad
Hi Suraj Tripathi thank you for your email But i am again getting error like "variable does not exist Id" Aamir Arshad Mob No:9939850598
AbhinavAbhinav (Salesforce Developers) 
Hi Aamir ,

There might be issue wth you Soql query and where clause

I tested you code replacing 
 
Profile p = [SELECT Id FROM Profile WHERE Name='Test'];
UserRole r = [SELECT Id FROM UserRole WHERE Name='Manager']
By 
Profile p = [SELECT Id FROM Profile limit 1];
UserRole r = [SELECT Id FROM UserRole limit 1];
and I am getting that Mixed Dml Exception. Try it

Code
 
public class Mixed_DML_Example {
    
    
    public  void callMe(){
           Profile p = [SELECT Id FROM Profile limit 1];
        UserRole r = [SELECT Id FROM UserRole  limit 1];
        // Create new user with a non-null user role ID 
        User u = new User(alias = 'welll', email='salesforcebatch111@gmail.com', 
            emailencodingkey='UTF-8', lastname='wilson11', 
            languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
            timezonesidkey='America/Los_Angeles', 
            username='wilsonDev@221Test.com');
        insert u;
            
            
            Account acc= new Account();
             acc.Name='Testing';
             acc.Phone='12345';
            acc.Rating='Hot';
            insert acc;
            
    }

}

If it helps mark it as best answer.

Thanks!
 
mukesh guptamukesh gupta
Hi Amir,

Reason:-

DML operations on certain sObjects, sometimes referred to as setup objects, can’t be mixed with DML on other sObjects in the same transaction. This restriction exists because some sObjects affect the user’s access to records in the org. You must insert or update these types of sObjects in a different transaction to prevent operations from happening with incorrect access-level permissions.  For example, you can’t update an account and a user role in a single transaction.

Apex class:- MixedDMLFuture    (Account will insert here)
public class MixedDMLFuture {
    public static void useFutureMethod() {
        // First DML operation
        Account a = new Account(Name='Acme');
        insert a;
        
        // This next operation (insert a user with a role) 
        // can't be mixed with the previous insert unless 
        // it is within a future method. 
        // Call future method to insert a user with a role.
        Util.insertUserWithRole(
            'mruiz@awcomputing.com', 'mruiz', 
            'mruiz@awcomputing.com', 'Ruiz');        
    }
}

Apex Class:-  Util    (user will insert here)
public class Util {
    @future
    public static void insertUserWithRole(
        String uname, String al, String em, String lname) {
 
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
        // Create new user with a non-null user role ID 
        User u = new User(alias = al, email=em, 
            emailencodingkey='UTF-8', lastname=lname, 
            languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
            timezonesidkey='America/Los_Angeles', 
            username=uname);
        insert u;
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
aamir arshadaamir arshad
Thanks a lot. Yes I know my code will give the mixdml exceptional but I am not getting that error message
Suraj Tripathi 47Suraj Tripathi 47

Hi Amir, 

You are getting Mixed Dml Exception because you are performing DML operation on both setup(Profile,User) and non setup object(Account).

SetUp Objects:Setup objects are objects that are used to interact with the metadata. Common example is the User, Profile, Layout object.
Non-SetUp Object: Every other objects like those which are native(Standard Objects) and Custom Objects fall into the category of Non-Setup Objects.
One important note is that we cannot perform DMLs on setup and non-setup objects in the same transaction. Workaround is that you need to use asynchronous requests (@future) or use batch as it runs in its own system context.

Thanks