You need to sign in to do that
Don't have an account?
deter dangler
Mixed DML operations in test method without System.runAs() method
I am new to salesforce, so trying every example that is given there on the helo document.
The below code is from the link https://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_qs_HelloWorld.htm|StartTopic=Content%2Fapex_qs_HelloWorld.htm|SkinName=webhelp
Am I doing something wrong??
I am just running my test class in mavensmate
The below code is from the link https://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_qs_HelloWorld.htm|StartTopic=Content%2Fapex_qs_HelloWorld.htm|SkinName=webhelp
@isTest private class MixedDML { static testMethod void mixedDMLExample() { User u; Account a; User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()]; // Insert account as current user System.runAs (thisUser) { Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; UserRole r = [SELECT Id FROM UserRole WHERE Name='COO']; u = new User(alias = 'jsmith', email='jsmith@acme.com', emailencodingkey='UTF-8', lastname='Smith', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, userroleid = r.Id, timezonesidkey='America/Los_Angeles', username='jsmith@acme.com'); insert u; a = new Account(name='Acme'); insert a; } } }As per the link, this code should throw error if i remove the block System.runAs() but it is working fine.
Am I doing something wrong??
I am just running my test class in mavensmate
I understand that what r u trying for. Actually System.runAs() used when we need a customer portal user for test class , means that in this case u have no user and u create a temp user for noly test class.
like :
@isTest
private class MixedDML {
static testMethod void mixedDMLExample() {
//Create Account
Account accObj = new Account(name='Test');
insert accObj;
//Create Contact
Contact con = new contact(lastname='test',email='test@gmail.com',accountid=accObj.id);
insert con;
Profile p = [SELECT Id FROM Profile WHERE Name=''];//Profile name of customer portal
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.EmailHeader.triggerUserEmail = false;
User u = new User(alias = 'standt', email=con.email,
emailencodingkey='UTF-8', lastname=con.lastname, firstname=con.firstname, languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, contactId=con.Id,
timezonesidkey='America/Los_Angeles', username=con.email);
u.setOptions(dmo);
insert u;
System.runAs(u) {
//Some work with this test user
}
}
}
In your code u are using real user so i think it will hrlpful.