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
StevekBlueSnapStevekBlueSnap 

Trigger causing errors with Data Loader

I have the following trigger deployed with the reference test class:

 

 

trigger LeadConvert on Lead (after insert,after update) {
//Bulkified
List<String> LeadNames = new List<String>{};
for(Lead myLead: Trigger.new){
 if((myLead.isconverted==false) && (myLead.Status == 'Confirmed')) {
Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Confirmed';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(false);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        }
        }
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

// Test Class for LeadConvert trigger

public class testLeadConvert {

    static testMethod void leadConvertTest() {
        // Create test data
        List<Lead> leadList = new List<Lead>();
        
        // Insert Leads
        for(integer i=0; i<10; i++){
            Lead l1 = new Lead(Company = 'Test Account#' +i , LastName= 'Test Lead LastName' +i, Status ='Confirmed');
            leadList.add(l1);
        }
        
        Profile p1 = [select Id from profile where name = 'Standard User'];
        User u = new User( Alias = 'standasf', 
                           Email='standarduser@testorg.com',
                           EmailEncodingKey='UTF-8',
                           LastName='Testing',
                           LanguageLocaleKey='en_US',
                           LocaleSidKey='en_US',
                           ProfileId = p1.Id,
                           TimeZoneSidKey='America/Los_Angeles',
                           UserName='stadfasdfsdfndarduser@testorg.com'
        );
        
        System.runAs(u){
            Test.startTest();
            Database.Insert(leadList);
            Test.stopTest();
        }
        //Assertion Testing
        leadList = [select Id, IsConverted, ConvertedAccountId, ConvertedContactId from Lead where Id IN :leadList];
        
        for(integer i=0;i<leadList.size();i++){
            System.assertEquals(leadList[i].IsConverted, true , 'Values not equal');
            System.assert(leadList[i].ConvertedAccountId <> null );
            System.assert(leadList[i].ConvertedContactId <> null );
        }
    }
}

 

 

 

Unfortunately I tried to Upsert from the Data Loader (over 9000 records) and got this error:

 

 Developer script exception from BlueSnap : LeadConvert : LeadConvert: execution of AfterUpdate  caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: LIMIT_EXCEEDED, System.LimitException: SFSSDupeCatcher:Too many S...

 

Apex script unhandled trigger exception by user/organization: 00570000001K82v/00D700000008Rkb

 

LeadConvert: execution of AfterUpdate

 

caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: LIMIT_EXCEEDED, System.LimitException: SFSSDupeCatcher:Too many SOQL queries: 101: []

 

Trigger.LeadConvert: line 11, column 1

 

What am I missing or do I need to change "after insert" or "after update"

 

The populated fields from the upsert have nothing to do with the Lead Status and therefore I'm not sure what is causing the error

Naidu PothiniNaidu Pothini
trigger LeadConvert on Lead (after insert,after update) 
{
//Bulkified List<Lead> convertRecords = new List<Lead>(); for(Lead myLead: Trigger.new) { if((myLead.isconverted==false) && (myLead.Status == 'Confirmed')) { convertRecords.add(myLead); } } if(convertRecords.size() > 0) { LeadConvert[] leadsToConvert = new LeadConvert[convertRecords.size()]; for(Integer i = 0 ; i < convertRecords.size(); i++) { leadsToConvert[i] = new LeadConvert(); leadsToConvert[i].setLeadId(convertRecords[i].Id); leadsToConvert[i].setConvertedStatus('Confirmed'); }
Database.LeadConvertResult lcr = Database.convertLead(leadsToConvert); } }

 try this.

StevekBlueSnapStevekBlueSnap

Without any edits to the class it prompts a Compile Error: Variable does not exist at line 21 column 9. This is my first trigger I've deployed and I do not have a Computer sciences background - any detailed explanation/reasoning would be appreciated :)

Naidu PothiniNaidu Pothini

I have updated the above post. please try it.

 

StevekBlueSnapStevekBlueSnap

New compile error line 22, column 27 :Variable does not exist" setLeadId - might eventually lead to a compile error on line 23 as it works through the string?

 

If I understood how it was trying to work I might be able to fix without asking, but I am lost.

 

My original trigger works, I just cannot use for batches larger than 100

Naidu PothiniNaidu Pothini

Can you try it again. i have updated the code.

StevekBlueSnapStevekBlueSnap
Method does not exist or incorrect signature: [LeadConvert].setLeadId(Id) at line 22 column 27
Naidu PothiniNaidu Pothini
trigger LeadConvert on Lead (after insert,after update) 
{
    List<Lead> convertRecords = new List<Lead>();

    for(Lead myLead: Trigger.new)
    {
        if((myLead.isconverted==false) && (myLead.Status == 'Confirmed')) 
    {
        convertRecords.add(myLead);
        }
    }

    if(convertRecords.size() > 0)
    {
        List<Database.LeadConvert> leadsToConvert = new List<Database.LeadConvert>();

        for(Lead ld : convertRecords)
        {
            Database.LeadConvert lc = new Database.LeadConvert();

            lc.SetLeadId(ld.Id);
            lc.SetConvertedStatus('Confirmed');

            leadsToConvert.add(lc);
        }

        List<Database.LeadConvertResult> lcr = Database.convertLead(leadsToConvert);
    }
}

 this will surely work. it worked for me.

StevekBlueSnapStevekBlueSnap

That did work - wouldn't I now need to adjust the Apex Class for testing?

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Test Class for LeadConvert2 trigger

public class testLeadConvert2 {

    static testMethod void leadConvertTest() {
        // Create test data
        List<Lead> leadList = new List<Lead>();
        
        // Insert Leads
        for(integer i=0; i<10; i++){
            Lead l1 = new Lead(Company = 'Test Account#' +i , LastName= 'Test Lead LastName' +i, Status ='Confirmed');
            leadList.add(l1);
        }
        
        Profile p1 = [select Id from profile where name = 'Standard User'];
        User u = new User( Alias = 'standasf', 
                           Email='standarduser@testorg.com',
                           EmailEncodingKey='UTF-8',
                           LastName='Testing',
                           LanguageLocaleKey='en_US',
                           LocaleSidKey='en_US',
                           ProfileId = p1.Id,
                           TimeZoneSidKey='America/Los_Angeles',
                           UserName='stadfasdfsdfndarduser@testorg.com'
        );
        
        System.runAs(u){
            Test.startTest();
            Database.Insert(leadList);
            Test.stopTest();
        }
        //Assertion Testing
        leadList = [select Id, IsConverted, ConvertedAccountId, ConvertedContactId from Lead where Id IN :leadList];
        
        for(integer i=0;i<leadList.size();i++){
            System.assertEquals(leadList[i].IsConverted, false , 'Values not equal');
            System.assert(leadList[i].ConvertedAccountId <> null );
            System.assert(leadList[i].ConvertedContactId <> null );
        }
    }
}

 

getting error at Line 36, column 1 when running test and trying to validate;

Failure Message: "System.AssertException: Assertion Failed: Values not equal: Expected: true, Actual: false", Failure Stack Trace: "Class.testLeadConvert.leadConvertTest: line 36, column 1"

StevekBlueSnapStevekBlueSnap

Also this error when Running test:

 

Class.testLeadConvert2.leadConvertTest: line 37, column 1

Naidu PothiniNaidu Pothini
@isTest
public class testLeadConvert2 
{
    static testMethod void leadConvertTest() 
    {
        List<Lead> leadList = new List<Lead>();
        
        for(integer i=0; i<10; i++)
        {
            Lead l1 = new Lead(Company = 'Test Account#' +i , LastName= 'Test Lead LastName' +i, Status ='Confirmed');
            leadList.add(l1);
        }
        
        Profile p1 = [SELECT Id FROM Profile WHERE Name = 'Standard User'];
        
        User u = new User( Alias = 'standasf', 
                           Email='standarduser@testorg.com',
                           EmailEncodingKey='UTF-8',
                           LastName='Testing',
                           LanguageLocaleKey='en_US',
                           LocaleSidKey='en_US',
                           ProfileId = p1.Id,
                           TimeZoneSidKey='America/Los_Angeles',
                           UserName='stadfasdfsdfndarduser@testorg.com');
                           
        insert u;
        
        System.runAs(u)
        {
            Test.startTest();
            Database.Insert(leadList);   
            Test.stopTest();
        }
        
        for(Lead ld : [SELECT Id, IsConverted, ConvertedAccountId, ConvertedContactId FROM Lead WHERE Id IN :leadList])
        {
            System.assertEquals(ld.IsConverted, true, 'Values not equal');
            System.assert(ld.ConvertedAccountId <> null);
            System.assert(ld.ConvertedContactId <> null);
        }
    }
}

 try this.

StevekBlueSnapStevekBlueSnap

received "expected false, received true message. Terurned to False and bow get this:

 

System.AssertException: Assertion FailedClass.testLeadConvert2.leadConvertTest: line 38, column 1

 

What System Assertion is being reference that it cannot find?

Naidu PothiniNaidu Pothini

You might be using the statement

 

System.assertEquals(ld.IsConverted, false, 'Values not equal');

 As it is expecting for false and as the lead is converted and value is true the condition is failing.

 

Try changing it to

 

System.assertEquals(ld.IsConverted, true, 'Values not equal');

 

StevekBlueSnapStevekBlueSnap

typed exactly as  instructed and this error:

 System.AssertException: Assertion Failed: Values not equal: Expected: false, Actual: trueClass.testLeadConvert2.leadConvertTest: line 37, column 1



back at false, the rror moves to next line regarding ContactId <> null

Naidu PothiniNaidu Pothini
for(Lead ld : [SELECT Id, IsConverted, ConvertedAccountId, ConvertedContactId FROM Lead WHERE Id IN :leadList])
{
    System.debug('------------- Is Converted check-----------'+ld.IsConverted); 
    System.debug('------------- Converted AccountId-----------'+ld.ConvertedAccountId);
    System.debug('------------- Converted ContactId-----------'+ld.ConvertedContactId);

System.assertEquals(ld.IsConverted, true, 'Values not equal'); System.assert(ld.ConvertedAccountId <> null); System.assert(ld.ConvertedContactId <> null); }

 Can you check the debug log and the values? For some reason, it worked perectly for me..

StevekBlueSnapStevekBlueSnap

added the debug lines and still this error:

 

 System.AssertException: Assertion Failed: Values not equal: Expected: false, Actual: trueClass.testLeadConvert2.leadConvertTest: line 41, column 1
StevekBlueSnapStevekBlueSnap

I downloaded debug log - but have no clue what to look for

Naidu PothiniNaidu Pothini
System.debug('------------- Is Converted check-----------'+ld.IsConverted); 
    System.debug('------------- Converted AccountId-----------'+ld.ConvertedAccountId);
    System.debug('------------- Converted ContactId-----------'+ld.ConvertedContactId);

 Look for the values in the three statements.

 

if possible can you post me the debug log. you can send the log in a private message.

 

StevekBlueSnapStevekBlueSnap
Rows:10 09:42:54.022 (1022921000)|SYSTEM_METHOD_ENTRY|[7]|QueryLocatorIterator.QueryLocatorIterator() 09:42:54.022 (1022937000)|SYSTEM_METHOD_EXIT|[7]|QueryLocatorIterator 09:42:54.023 (1023249000)|USER_DEBUG|[37]|DEBUG|------------- Is Converted check-----------false 09:42:54.023 (1023296000)|USER_DEBUG|[38]|DEBUG|------------- Converted AccountId-----------null 09:42:54.023 (1023340000)|USER_DEBUG|[39]|DEBUG|------------- Converted ContactId-----------null 09:42:54.023 (1023441000)|EXCEPTION_THROWN|[41]| System.AssertException: Assertion Failed: Values not equal: Expected: false, Actual: true 09:42:54.023 (1023692000)|FATAL_ERROR|System.AssertException: Assertion Failed: Values not equal: Expected: false, Actual: true Class.testLeadConvert2.leadConvertTest: line 41, column 1 09:42:54.023 (1023709000)|FATAL_ERROR|System.AssertException: Assertion Failed: Values not equal: Expected: false, Actual: true
Naidu PothiniNaidu Pothini

can you try the code i have sent you in the Private message.