[SalesForce] Keep getting this error: System.QueryException: List has no rows for assignment to SObject

I have a Trigger that is referencing a class. It works in Dev, but now I am trying to create the test class in order to push it to production. I am trying to test that a new user is created. I keep getting the same error System.QueryException: List has no rows for assignment to SObject. The error seems to point to the class on line 9 where the Account is added. The goal of the code is when a new user is inserted, then a contact for that new user is created as well. Does someone have any ideas why this is happening? I've used the Query Editor to make sure that the record does exist, and it does, so I'm stumped. Thanks in advance for the help.

Trigger

    trigger UserInsertContactInsert1 on User (after insert, after update) {

    if(Trigger.isAfter && Trigger.isInsert){
        Set<ID> usrIds = new Set<ID>();

        for(User u : Trigger.new){
            usrIds.add(u.id);
        }

        UserContactSyncClass1.syncContact(usrIds);  
    }

    if(Trigger.isAfter && Trigger.isUpdate){ 
        Set<ID> idSet = new Set<ID>();
        Set<ID> idSet2 = new Set<ID>();

        for(User u : trigger.new){
            for(User r : trigger.old){
                if(u.ID == r.ID && u.isActive==false && r.isActive==true){
                    idSet.add(u.ID);
                }

                if (u.ID == r.ID && u.isActive==true && r.isActive==false) {
                    idSet2.add(u.ID);
                } 
            }
        }

        UserContactSyncClass1.InactiveContact(idSet);
        UserContactSyncClass1.ActivateContact(idSet2);
    }
}

Class

public class UserContactSyncClass1 {

    @future
    public static void syncContact(Set<Id> userSet){
        List<Contact> cList = new List<Contact>();
        List<User> userList = [Select ID, FirstName, LastName, Email, Phone, True_Market_Unit__c, True_Market_Name__c FROM User WHERE ID IN :userSet];
        List<String> mList = new List<String>();
        List<String> mktList = new List<String>();
        **Account acc = [SELECT Id, Name FROM Account WHERE Name = 'Company Users' Limit 1];**

        // Loop through Users, to get List of True Market Number
        for(User usr : userList){
            mList.add(usr.True_Market_Unit__c);        
        }

        // Use the True Market Number and Match to the True Market Number in Markets
        for(Market__c mkt : [SELECT Name, Unit_Number__c FROM Market__c WHERE Unit_Number__c IN :mList]){
            mktList.add(mkt.Name);  
        }

        for(User usr : userList){
            for(String mk : mktList){
                usr.True_Market_Name__c = mk;
            }
            System.debug(usr.True_Market_Name__c);
        }

        for(User u : userList){
            cList.add(new Contact(FirstName=u.FirstName, LastName=u.LastName, User_True_Market_Name__c=u.True_Market_Name__c, Email=u.Email, Phone=u.Phone, OwnerID=u.ID, Related_User__c=u.Id, AccountId = acc.Id));
        }

        upsert cList;
    }

Test Class Method with the Error

public static testMethod void validateNewUser(){

        Profile p = [SELECT id FROM Profile WHERE Name='System Administrator'];

        User u = new User(
            FirstName = 'test', 
            LastName= 'last_test', 
            Email='test323232@test.com', 
            Phone='111-111-1111', 
            True_Market_Unit__c='1111', 
            alias = 'test', 
            EmailEncodingKey='UTF-8',
            ProfileId = p.Id,
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US', 
            TimeZoneSidKey='America/Los_Angeles',
            UserName = 'test323232@test.com' 
        );

        System.runAs(u){
            System.assertEquals('111-111-1111', u.Phone);
        }

    }

Updated Solution

Turns out even though my User never referenced the account, I still needed to insert it into my test class, with the Name='Company Users' from my SOQL query. Thanks everyone for all the help.

Best Answer

Test classes do not automatically have access to Org data. Usually you have to create it as part of the test class. If you absolutely must use Org data then you can use the @isTest(SeeAllData=true) tag on your test class.

Related Topic