+ Start a Discussion

Apex Trigger Testing - Updating Custom Setting

Good Morning,

First, I must admit this is my first time working with Apex code so any assistance that you can offer is VERY much appreciated.

After having worked my way through Salesforce tutorial on Apex and receiving some assistance from the help desk at Salesforce, I managed to put together the following Apex trigger:

trigger leadAssignment on Lead (before insert) {
    for(lead lea : trigger.new) {
        Boolean isLeadEnd = lea.Distributor_Or_End_User__c.equals('End User');
        if (isLeadEnd) {
        List<leadRound__c> mcs1 = new List<leadRound__c>();
        leadRound__c mcs = [SELECT Id, name__c, LeadRoundNm__C FROM leadRound__c WHERE name__c='leadround'];
        string textField='';
        integer LA;
        if (mcs.name__c == 'leadround') {
            textField = mcs.name__c;
            LA = integer.valueOf(mcs.LeadRoundNm__c);
       for(lead lea2 : trigger.new) {
            integer round;
                    integer arry = round -1;
                    integer LA1 = LA + 1;
                        LA1 = 1;
                    else {
                        mcs.LeadRoundNm__c = decimal.valueOf(LA1);
    update mcs1;

The purpose of this trigger is to update a custom setting number field that we can refer to when assigning incoming End User leads via round robin (we cannot do the typical round robin custom formula based on an autonumber as we want our distributor leads to be assigned to specific users).

The trigger appears to be working fine in the development site and functions just as it should. However, the issue I am running into presently is in preparing the test class for this trigger. I have prepared the following test class and have run tests on it, but to no avail. Here is the test class I prepared:


public class RoundRobinLeadTest{
static testMethod void myUnitTest ()
        Boolean updated;
        integer roundBefore;
        integer roundAfter;
        List<leadRound__c> old1 = new List<leadRound__c>();
        leadRound__c old = [SELECT LeadRoundNm__c FROM leadRound__c];
        roundBefore = integer.valueOf(old.leadRoundNm__c);
        //Setup the lead record
        Lead lead = new Lead();
        lead.LastName = 'last';
        lead.FirstName = 'First';
        lead.company = 'Company';
        lead.Status = 'Open';
        lead.IsUnreadByOwner = true;
        lead.Distributor_or_End_User__c = 'End User';
        insert lead;
        leadRound__c new1 = [SELECT LeadRoundNm__c FROM leadRound__c];
        roundAfter = integer.valueOf(new1.leadRoundNm__c);
        if (roundBefore < roundAfter) {
            updated = true;

Here is the Apex Test Result Detail that is returned:

Class = RoundRobinLeadTest
Method Name = myUnitTest
Pass/Fail = Fail
Error Message = System.QueryException: List has no rows for assignment to SObject
Stack Trace = Class.RoundRobinLeadTest.myUnitTest: line 10, column 1

What do I need to change in the test class code above to resolve this error and (hopefully) get my Apex Trigger rolled out on my production site?

Once again, I cannot say how greatful I am for the assistance here! Thanks!!
Adnubis LLCAdnubis LLC
When you are writing test coverage by default you do not have access to any real data. You are getting an error because you are trying to run a query on the leadRound__c records and there are none to be returned. There are two directions you can go here. You can alter test annotation with the following


or you can create some leadRound__c objects in your test coverage and insert them instead of querying for them. To give you my two cents on which way to go I would suggest the second solution. Your testing should not require records to exist outside of your test enviroment. It would allow an outside influence on the testing.

Hope this helps.

Hi webmaster1 - 

I'm not an expert, but hopefully I can help.

I beleive you are getting that error becaues you are trying to assign a list of leadRound objects to a specific instance of a leadRound object (SOQL queries always return a list)

List<leadRound__c> old1 = new List<leadRound__c>();
        leadRound__c old = [SELECT LeadRoundNm__c FROM leadRound__c];

You can use a SOQL query assign to an instance of your object by adding the LIMIT clause to your query or by choosing which list item you want to assign:

 leadRound__c old = [SELECT LeadRoundNm__c FROM leadRound__c LIMIT 1]; This will guarantee only one record is returned from your query and allow you to assign that record to your leadRound instance


leadRound__c old = [SELECT LeadRoundNm__c FROM leadRound__c][0]; This will return all leadRound records from the query and allow you to choose which list item to assign to your leadRound instance

.  If you need a specific leadRound record for your test you will want to add a WHERE clause to your query.

Good luck!

Thank you both for offering your insight into the matter!

Adnubis - You mention that we are unable to access any actual data and leadRound__c must be created within the test. I was attempting to draw this information from a custom setting who has a custom number field called leadRoundNm__c with a default value of 1. Are you saying I will I need to write code that will generate a new custom setting with a custom number field? Or can I, as acl5, proposes above call on this default instance from within the test code?