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
BobBob 

Test Class List has no rows for assignement

My test class below keeps coming up with an error when i triy to move an updated trigger over to my production org via outbound change set or eclipse. The error is List has no rows for assignement. If anyone can help find the reason this test class keeps coming up with an error, i would appreciate it.
User-added image

/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 *
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */

@isTest
private class Account_Create_Contact_TestClass {

    static testMethod void myUnitTest() {
       
        Profile p = [SELECT Id FROM Profile WHERE Name = 'BLX Telemarketer' LIMIT 1];
        User u = [SELECT Id FROM User WHERE ProfileId = :p.Id AND User.IsActive = true LIMIT 1];
        List<Account> accs = new List<Account>();
        List<Account> conAccs = new List<Account>();
        Set<Id> accIds = new Set<Id>();
       
        Integer i = 0;
        System.runAs(u) {
           
            // Test Accounts with no Last Name has been added
            for (Account a : [SELECT Id FROM Account LIMIT 50]) {
                a.Eerstenaam__c = 'Test First Name';
            }
           
            if (!accs.isEmpty()) {
                update(accs);
                accs.clear();
            }
                   
            for (Account a : [SELECT Id FROM Account LIMIT 50]) {
                accIds.add(a.Id);
                a.Eerstenaam__c = 'Test First Name';
                a.Achternaam__c = 'Test Last Name';
                a.Nieuwsbrieven_ontvangen__c = false;
                if (i == 0) {
                    a.Nieuwsbrieven_Ontvangen__c = true;
                    a.Dezelfde_Email__c = true;
                    i = 1;
                }
                else if (i == 1) {
                    i = 2;
                    a.Status_Belactie_2010__c = 'Wil Gebeld Worden Door Account Manager';
                    a.Opmerkingen__c = 'This is a test Task';
                }
                else {
                    a.Achternaam__c = NULL;
                }
                accs.add(a);
            }
            if (!accs.isEmpty()) {
                update(accs);
            }
           
            for (Contact c : [SELECT Id, LastName, AccountId FROM Contact WHERE AccountId IN :accIds AND LastName = 'Test Last Name']) {
                Account a = new Account(Id=c.AccountId);
                a.Achternaam__c = c.LastName;
                conAccs.add(a);
            }
           
            if (!conAccs.isEmpty()) {
                update(conAccs);
            }          
        }
               
       
    }
}
Best Answer chosen by Bob
Apoorv Saxena 4Apoorv Saxena 4
Hi Bob,

Try this code :
 
@isTest
private class Account_Create_Contact_TestClass {

    static testMethod void myUnitTest() {
       
        Profile p = [SELECT Id FROM Profile WHERE Name = 'BLX Telemarketer' LIMIT 1];
        User u = new User(Alias = 'newUserTest213Bob', Email='newBoB213user@test.com',EmailEncodingKey='UTF-8', LastName='TestingBob', anguageLocaleKey='en_US',LocaleSidKey='en_US', ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='newBoB213user@test152org.com');
        insert u;
        List<Account> accs = new List<Account>();
		List<Account> accList = new List<Account>();
        List<Account> conAccs = new List<Account>();
        Set<Id> accIds = new Set<Id>();
        
		Account acc = new Account();
		for(i=1;i<=50;i++){
		acc.name = 'Test Acc'+i;
		accList.add(acc);
		}
		insert accList;
		
		Contact con = new Contact();
		con.lastname = 'Test Last Name';
		con.accountId = accList[0].id;
		insert con;
		
        Integer i = 0;
        System.runAs(u) {
           
            // Test Accounts with no Last Name has been added
            for (Account a : [SELECT Id,Eerstenaam__c FROM Account LIMIT 50]) {
                a.Eerstenaam__c = 'Test First Name';
            }
           
            if (!accs.isEmpty()) {
                update(accs);
                accs.clear();
            }
                   
            for (Account a : [SELECT Id,Eerstenaam__c,Achternaam__c,Nieuwsbrieven_ontvangen__c FROM Account LIMIT 50]) {
                accIds.add(a.Id);
                a.Eerstenaam__c = 'Test First Name';
                a.Achternaam__c = 'Test Last Name';
                a.Nieuwsbrieven_ontvangen__c = false;
                if (i == 0) {
                    a.Nieuwsbrieven_Ontvangen__c = true;
                    a.Dezelfde_Email__c = true;
                    i = 1;
                }
                else if (i == 1) {
                    i = 2;
                    a.Status_Belactie_2010__c = 'Wil Gebeld Worden Door Account Manager';
                    a.Opmerkingen__c = 'This is a test Task';
                }
                else {
                    a.Achternaam__c = NULL;
                }
                accs.add(a);
            }
            if (!accs.isEmpty()) {
                update(accs);
            }
           
            for (Contact c : [SELECT Id, LastName, AccountId FROM Contact WHERE AccountId IN :accIds AND LastName = 'Test Last Name']) {
                Account a = new Account(Id=c.AccountId);
                a.Achternaam__c = c.LastName;
                conAccs.add(a);
            }
           
            if (!conAccs.isEmpty()) {
                update(conAccs);
            }          
        }
               
       
    }
}


Please let me know how this works for you, mark this as Solved if this helps you so that others can view it as a proper solution.

Thanks,
​Apoorv

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Bob,

Try this code :
 
@isTest
private class Account_Create_Contact_TestClass {

    static testMethod void myUnitTest() {
       
        Profile p = [SELECT Id FROM Profile WHERE Name = 'BLX Telemarketer' LIMIT 1];
        User u = new User(Alias = 'newUserTest213Bob', Email='newBoB213user@test.com',EmailEncodingKey='UTF-8', LastName='TestingBob', anguageLocaleKey='en_US',LocaleSidKey='en_US', ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='newBoB213user@test152org.com');
        insert u;
        List<Account> accs = new List<Account>();
		List<Account> accList = new List<Account>();
        List<Account> conAccs = new List<Account>();
        Set<Id> accIds = new Set<Id>();
        
		Account acc = new Account();
		for(i=1;i<=50;i++){
		acc.name = 'Test Acc'+i;
		accList.add(acc);
		}
		insert accList;
		
		Contact con = new Contact();
		con.lastname = 'Test Last Name';
		con.accountId = accList[0].id;
		insert con;
		
        Integer i = 0;
        System.runAs(u) {
           
            // Test Accounts with no Last Name has been added
            for (Account a : [SELECT Id,Eerstenaam__c FROM Account LIMIT 50]) {
                a.Eerstenaam__c = 'Test First Name';
            }
           
            if (!accs.isEmpty()) {
                update(accs);
                accs.clear();
            }
                   
            for (Account a : [SELECT Id,Eerstenaam__c,Achternaam__c,Nieuwsbrieven_ontvangen__c FROM Account LIMIT 50]) {
                accIds.add(a.Id);
                a.Eerstenaam__c = 'Test First Name';
                a.Achternaam__c = 'Test Last Name';
                a.Nieuwsbrieven_ontvangen__c = false;
                if (i == 0) {
                    a.Nieuwsbrieven_Ontvangen__c = true;
                    a.Dezelfde_Email__c = true;
                    i = 1;
                }
                else if (i == 1) {
                    i = 2;
                    a.Status_Belactie_2010__c = 'Wil Gebeld Worden Door Account Manager';
                    a.Opmerkingen__c = 'This is a test Task';
                }
                else {
                    a.Achternaam__c = NULL;
                }
                accs.add(a);
            }
            if (!accs.isEmpty()) {
                update(accs);
            }
           
            for (Contact c : [SELECT Id, LastName, AccountId FROM Contact WHERE AccountId IN :accIds AND LastName = 'Test Last Name']) {
                Account a = new Account(Id=c.AccountId);
                a.Achternaam__c = c.LastName;
                conAccs.add(a);
            }
           
            if (!conAccs.isEmpty()) {
                update(conAccs);
            }          
        }
               
       
    }
}


Please let me know how this works for you, mark this as Solved if this helps you so that others can view it as a proper solution.

Thanks,
​Apoorv
This was selected as the best answer
BobBob
I am getting the following error
Error: Compile Error: Invalid field anguageLocaleKey for SObject User at line 7 column 153
Apoorv Saxena 4Apoorv Saxena 4
Hi Bob,

Make it LanguageLocaleKey, I missed the 'L'.
Let me know how it works out for you.
BobBob
Fixed it and then tried to send it to proudction via outbound change set and recevied a validation error when validating.
System.Exception: Assertion Failed: Expected: 111.11, Actual: 999.99
Stack Trace: Class.Update_Opportunity_Amount_TestClass.myUnitTest: line 94, column 1
Apoorv Saxena 4Apoorv Saxena 4
Hi Bob,

The error thrown is due to some other class. What you need to do is while deploying your change set is to Run Specified Test instead of running all tests and in the textbox, specify your test class name and then deploy.That ways you wont face any trouble.

Hope this helps!

Please mark this question as Solved if you question was answered here, so that it'll make a proper solution for other community members.

Thanks,
Apoorv
BobBob
That worked. Thank you