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
lp okllllp oklll 

Urgent help required: System.QueryException: List has no rows for assignment to SObject

Hi,

I am getting the error message System.QueryException: List has no rows for assignment to SObject
MY  TRIGGER is as follows:

trigger contactRecOwnerUpdate on Contact (before insert)
{
    // This trigger sets the owner of the contact record to be the same as the
    // owner of the account record
    //
    // Version 1.0    
    
    Map<String, Contact> contactMap = new Map<String, Contact>();
    
    for (Contact contact : Trigger.New) 
    {
        if(Trigger.isBefore)
        {
            if (System.Trigger.isInsert)
            {
                // Find the OwnerID of the account that the contact is going to be associated to
                Account contactAccount = [select OwnerId from Account where Id=:contact.AccountId];

                // Make the contact owner the same as the account
                contact.OwnerId = contactAccount.OwnerId;

                // Insert the OwnerId into the contact record
                contactMap.put(contact.OwnerId,contact);
            }
        }

    }
}


And MY Class  is below:

@isTest
private class TestApexTriggers
{
    static testMethod void apexTriggersUnitTest()
    {
        //Set up user
        Profile supportUser = [SELECT Id FROM Profile WHERE Name='U K Support User'];
        Profile supportUserTL = [SELECT Id FROM Profile WHERE Name='SENIOR MANAGER'];
        User nordicTL = [SELECT Id FROM User WHERE ProfileId=:supportUserTL.Id and Alias ='ball'];
        
        // The alias will change when the correct users are created
        User nordicAgent = [SELECT Id FROM User WHERE ProfileId=:supportUser.Id and Alias ='rbows'];

        // Get a list of all active record types within the system associated to Accounts
        List<RecordType> accRTypes = [Select Name, Id From RecordType where sObjectType='Account'];

        // Create a map between the Account record Type Name and Id for for easy retrieval
        Map<String,String> accountRecordTypes = new Map<String,String>{};
        for(RecordType accRT: accRTypes)
            accountRecordTypes.put(accRT.Name,accRT.Id);

        // Get a list of all active record types within the system associated to Contacts
        List<RecordType> conRTypes = [Select Name, Id From RecordType where sObjectType='Contact'];

        // Create a map between the Account record Type Name and Id for for easy retrieval
        Map<String,String> contactRecordTypes = new Map<String,String>{};
        for(RecordType conRT: conRTypes)
            contactRecordTypes.put(conRT.Name,conRT.Id);

        //Run As nordicTL
        System.RunAs(nordicTL)
        {
            // Test Reseller create
            System.debug('****** START Reseller Test *******');
            System.debug('Expected User: System Administrator');
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('Current Profile: ' + UserInfo.getProfileId());
            
            Account resellerAcc = new Account();
            
            resellerAcc.Name = 'Unit Test Reseller1';
            resellerAcc.RecordTypeId = accountRecordTypes.get('Reseller');
            resellerAcc.Reseller_s_Hub_PIN__c = '9999';
            resellerAcc.ShippingStreet = 'Test Street1';
            resellerAcc.ShippingCity = 'Test City1';
            resellerAcc.ShippingState = 'Test State1';
            resellerAcc.ShippingCountry = 'Denmark';
            resellerAcc.ShippingPostalCode = '4700';

            System.debug('******* Trying insert resellerAcc *******');
            insert resellerAcc;
            System.debug('******* Inserted resellerAcc *******');
            
            System.debug('****** END Reseller Test *******');
        }
        
        //Run As nordicTL
        System.RunAs(nordicAgent)
        {
            System.debug('****** START Home Test *******');
            
            System.debug('****** Creating a list of resellers in the system *******');
            // Get a list of all Resellers within the system associated to Contacts
            List<Account> resellersList = [Select Name, Id From Account where RecordTypeId=:accountRecordTypes.get('Reseller')];

            System.debug('****** Creating a map of name, id for the retrieved resellers *******');
            // Create a map between the Reseller Name and Id for for easy retrieval
            Map<String,String> resellers = new Map<String,String>{};
            for(Account resAcc: resellersList)
                resellers.put(resAcc.Name,resAcc.Id);            

            // Test Home create
            System.debug('Expected User: Support User');
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('Current Profile: ' + UserInfo.getProfileId());
            
            Account homeAcc = new Account();
            
            homeAcc.Name = 'Unit Test Home1';
            homeAcc.RecordTypeId = accountRecordTypes.get('Home');
            homeAcc.Reseller__c = resellers.get('Unit Test Reseller1');
            homeAcc.ShippingStreet = 'Test Street1';
            homeAcc.ShippingCity = 'Test City1';
            homeAcc.ShippingState = 'Test State1';
            homeAcc.ShippingCountry = 'Denmark';
            homeAcc.ShippingPostalCode = '4700';
        
            System.debug('****** Trying insert homeAcc *******');
            insert homeAcc;
            System.debug('****** Inserted homeAcc *******');
            
            System.debug('****** END Home Test *******');

            // Test Contact create
            System.debug('****** START Contact Test *******');
            System.debug('****** Creating a list of Home accounts in the system *******');
            // Get a list of all Resellers within the system associated to Contacts
            List<Account> homesList = [Select Name, Id From Account where RecordTypeId=:accountRecordTypes.get('Home')];

            System.debug('****** Creating a map of name, id for the retrieved homes *******');
            // Create a map between the Home Account Name and Id for for easy retrieval
            Map<String,String> homes = new Map<String,String>{};
            for(Account homAcc: homesList)
                homes.put(homAcc.Name,homAcc.Id);            

            Contact homeCon = new Contact();
            
            homeCon.AccountId = homes.get('Unit Test Home1');
            homeCon.RecordTypeId = contactRecordTypes.get('Consumer');
            homeCon.Salutation = 'Mr1';
            homeCon.FirstName = 'Apex1';
            homeCon.LastName = 'Unit-Test1';

            System.debug('****** Trying insert homeCon *******');
            insert homeCon;
            System.debug('****** Inserted homeCon *******');
            
            System.debug('****** END Contact Test *******');            
        }
    }
}


 
Deepak BalurDeepak Balur
The issue is in your test class per me around these lines, the association ID is not coming thru from your test class.

            // Test Contact create
            System.debug('****** START Contact Test *******');
            System.debug('****** Creating a list of Home accounts in the system *******');
            // Get a list of all Resellers within the system associated to Contacts
            List<Account> homesList = [Select Name, Id From Account where RecordTypeId=:accountRecordTypes.get('Home')];

            System.debug('****** Creating a map of name, id for the retrieved homes *******');
            // Create a map between the Home Account Name and Id for for easy retrieval
            Map<String,String> homes = new Map<String,String>{};
            for(Account homAcc: homesList)
                homes.put(homAcc.Name,homAcc.Id);            

            Contact homeCon = new Contact();
            
            homeCon.AccountId = homes.get('Unit Test Home1');
lp okllllp oklll
So how do I sort this issue Deepak?
Deepak BalurDeepak Balur
Your test class is to ensure that the code will run. Try passing the record Id of 'Unit Test Home' directly in homeCon.AccountId and see if your code goes thu. You can come back at a later date and sort your TestClass.