You need to sign in to do that
Don't have an account?
aamir 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;
}
}
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;
}
}
Greetings!
In your query, 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-
If you find your Solution then mark this as the best answer.
Thank you!
Regards,
Suraj Tripathi
There might be issue wth you Soql query and where clause
I tested you code replacing
By and I am getting that Mixed Dml Exception. Try it
Code
If it helps mark it as best answer.
Thanks!
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)
Apex Class:- Util (user will insert here)
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
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