• webmaster1.3936162685669897E12
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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;
            for(round=1;round<=3;round++)
            {
                if(round==LA)
                {
                    integer arry = round -1;
                    integer LA1 = LA + 1;
             
                    if(LA1>3)
                    {
                        LA1 = 1;
                        mcs.LeadRoundNm__c=decimal.valueOf(LA1);
                        mcs1.add(mcs);
                    }
                    else {
                        mcs.LeadRoundNm__c = decimal.valueOf(LA1);
                        mcs1.add(mcs);
                    }
                }
            }
        }
    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:

@isTest

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);
       
        test.startTest();
        //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;
        }
        System.assert(updated);
        test.stopTest();
    }
}

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!!
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;
            for(round=1;round<=3;round++)
            {
                if(round==LA)
                {
                    integer arry = round -1;
                    integer LA1 = LA + 1;
             
                    if(LA1>3)
                    {
                        LA1 = 1;
                        mcs.LeadRoundNm__c=decimal.valueOf(LA1);
                        mcs1.add(mcs);
                    }
                    else {
                        mcs.LeadRoundNm__c = decimal.valueOf(LA1);
                        mcs1.add(mcs);
                    }
                }
            }
        }
    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:

@isTest

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);
       
        test.startTest();
        //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;
        }
        System.assert(updated);
        test.stopTest();
    }
}

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!!