I have written a class that is called from different triggers. This class accepts a list of api fields and an object name. The class will go through the api fields and check to see if the old value has changed from the new value upon updating a record of interest. If it does emails are sent and the change data is archived into a new object.

My problem is writing a new test method for this class. My trigger is lightweight and all the business logic is done in the class. I am only interested in sending out emails and archiving the change data on two conditions. The record is being updated by someone other than the original owner, OR the user is not in a list of Account Teams. I have tested my code and everything is good.

However, I'm struggling to get my test class to work. I have read about impersonating different users using the system.runAs method. Here is a snippet of my code.

        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 

    User u1 = new User(Alias = 'newUser', Email='[email protected]', 
    EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
    LocaleSidKey='en_US', ProfileId = p.Id,
    TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');

    User u2 = new User(Alias = 'newUser2', Email='[email protected]', 
    EmailEncodingKey='UTF-8', LastName='Testing2', LanguageLocaleKey='en_US', 
    LocaleSidKey='en_US', ProfileId = p.Id,
    TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');        
      System.runAs(u1) {
        Contact newContact = new Contact(FirstName='Billy', LastName='Smith', HomePhone='4444444444');
        insert newContact;
    System.runAs(u2) {        
        Contact contact = [Select FirstName, LastName from Contact limit 1];
        update contact;

The new contact is inserted into the db under the new user, but when I try to retrieve that record from a different user SFDC is unable to find the record I inserted. I tried getting it by newContact.id FirstName='Billy' and even just limit 1 since this is the only record inserted. It's like SFDC removes the data inserted by user 1 when user 2 tries to retrieve it. because of this I'm unable to write my test class. has anyone seen this before? am I doing something wrong or is this a bug in SFDC?

  • 1
    What are your Org Wide Sharing settings for Accounts and Contacts? If its private then User 2 won't be able to see Contacts crated by User 1. Commented Jun 19, 2014 at 22:02
  • also - are you sure that the first runAs Contact actually saved without dml error (validation rule such as "must have an email address?") - you might also want to remove the identity of your company from the businessUnit__c assignment
    – cropredy
    Commented Jun 19, 2014 at 23:33
  • Ah, the dreaded security model settings! I was hoping to put these changes in early before we relax the security model...I guess I could put some type of a condition for my test case so the logic could run. You are exactly right Alex, that is my my problem. Thank you all. First time I've used Stackexchange. thanks for your help.
    – Ryan
    Commented Jun 20, 2014 at 13:16
  • I've quickly converted my comment into an answer, please accept it to help other people who are facing the same problem. Thanks. Commented Jun 20, 2014 at 13:20

2 Answers 2


Are you Org Wide Sharing settings for Accounts & Contacts set to private? If so u2 won't be able to see records created by u1 as System.runAs(...) enforces sharing rules.

This is covered in the documentation here:

Generally, all Apex code runs in system mode, where the permissions and record sharing of the current user are not taken into account. The system method runAs enables you to write test methods that change the user context to an existing user or a new user so that the user’s record sharing is enforced. The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.


Contacts without an Account are considered private regardless of sharing settings. Try associating the Contact with an Account that both users can see.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .