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
prbprb 

Simulate a profile in a trigger unit test

I have a trigger on the Contract object.  The contract object has a validation rule that only allows certain profiles to add a record.  My unit test fails for the trigger even though Im running the test under an administrative account.

Is there a way to add the unit test profile to the validation rule?

This doesn't seem right. 

What seems better is to somehow simulate a mock profile that would have permission to pass the test.

Could someone please offer some guidance on the best practice?

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

I think the actual problem is with your validation rule.  Does it work correctly if you add the object via the standard interface, without the trigger? 

I think that rule would always fail,because you have all of the conditions "and" together, and there is no way for the profileid to be 2 different values.  when the profile id does equal 00e30000000e95EAAQ then the $Profile.Id <> '00e30000000e95FAAQ',would be true, and the error would fire.

 

you probably want something like this:

 

AND( ISNEW(), NOT(OR($Profile.Id == '00e30000000e95EAAQ', $Profile.Id == '00e30000000e95FAAQ')) )

 

 

 

 

All Answers

micwamicwa

You can select create a User with the profile you would like to test and run the testmode using this users permission

 

 

Profile profile = [select id from profile where name='x']; User user = new User(alias = 'test', email='test@test.com', emailencodingkey='UTF-8', lastname='test', languagelocalekey='en_US', localesidkey='en_US', profileid = profile .Id, timezonesidkey='America/Los_Angeles', username='test@testSF.com'); System.runAs(user) { //do whatever you would like to thest

}

 

 

 

prbprb

This is exactly the type of solution I was looking for. 

Unfortunately this doesn't work.

 

Here's my validation rule:

 

AND(
ISNEW(),
$Profile.Id <> '00e30000000e95EAAQ',
$Profile.Id <> '00e30000000e95FAAQ',
)

 

the code I tried was:

 

 Profile profile = [select id from profile where id='00e30000000e95EAAQ'];

User user = new User(alias = 'tester', email='tester420@test.com',emailencodingkey=

'UTF-8', lastname='test', languagelocalekey='en_US',

localesidkey='en_US', profileid = profile.Id,timezonesidkey=

'America/New_York', username='testSF@test.com');

system.debug('PMN: profile.Id =>'+profile.Id);

System.runAs(user) {

   //my insert fails here w/validation error 

 

Does anyone have any idea why the runAs user doesn't pass this validation test?

 

JimRaeJimRae
What error are you getting?  Is the validation failing, or is there some other issue?
prbprb

The validation is failing.

 

FIELD_CUSTOM_VALIDATION_EXCEPTION, "My validation message"

 

 

JimRaeJimRae

I think the actual problem is with your validation rule.  Does it work correctly if you add the object via the standard interface, without the trigger? 

I think that rule would always fail,because you have all of the conditions "and" together, and there is no way for the profileid to be 2 different values.  when the profile id does equal 00e30000000e95EAAQ then the $Profile.Id <> '00e30000000e95FAAQ',would be true, and the error would fire.

 

you probably want something like this:

 

AND( ISNEW(), NOT(OR($Profile.Id == '00e30000000e95EAAQ', $Profile.Id == '00e30000000e95FAAQ')) )

 

 

 

 

This was selected as the best answer
prbprb

This was indeed the problem.

Thanks everyone for your help.