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
Swami DeshanelSwami Deshanel 

Question regarding Mass Convert Trigger

I have a mass convert trigger that when a lead is a Contacted it will get converted.   When I test the trigger by changing the status of a lead to contacted, it works perfectly.   However, when I mass upload leads, it converts all the records, not just those with a Contacted status.   Please help

And on a side note, if you happen to have a test class that will conver this triggfer, that would save me heaps of time.  Thanks in advance

Code Below:

trigger AutoConvert on Lead (after update) {
    list<Lead> LeadsToConvert = new list<Lead>();
    for(Lead myLead: Trigger.new){
        if(!myLead.isConverted)
            LeadsToConvert.add(myLead);
    }

    list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
    for(Lead myLead : LeadsToConvert){
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Contacted';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(true);
        leadConverts.add(lc);
    }

    if(!leadConverts.isEmpty()){
        for(Integer i = 0; i <= leadConverts.size()/100 ; i++){
            list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
            Integer startIndex = i*100;
            Integer endIndex = ((startIndex+100) < leadConverts.size()) ? startIndex+100: leadConverts.size();
            for(Integer j=startIndex;j<endIndex;j++){
                tempList.add(leadConverts[j]);
            }
            Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
            for(Database.LeadConvertResult lcr : lcrList)
                System.assert(lcr.isSuccess());
        }
    }
}
ShashankShashank (Salesforce Developers) 
The code is not checking the status of the lead. Try this:
 
trigger AutoConvert on Lead (after update) {
    list<Lead> LeadsToConvert = new list<Lead>();
    for(Lead myLead: Trigger.new){
        if(!myLead.isConverted && (myLead.status=='Contacted'))
            LeadsToConvert.add(myLead);
    }

    list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
    for(Lead myLead : LeadsToConvert){
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Contacted';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(true);
        leadConverts.add(lc);
    }

    if(!leadConverts.isEmpty()){
        for(Integer i = 0; i <= leadConverts.size()/100 ; i++){
            list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
            Integer startIndex = i*100;
            Integer endIndex = ((startIndex+100) < leadConverts.size()) ? startIndex+100: leadConverts.size();
            for(Integer j=startIndex;j<endIndex;j++){
                tempList.add(leadConverts[j]);
            }
            Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
            for(Database.LeadConvertResult lcr : lcrList)
                System.assert(lcr.isSuccess());
        }
    }
}

 
Swami DeshanelSwami Deshanel
Thanks for getting back to me on this.   The code saved just fine, but when I tested it with a single lead in the system, i got this error:

Error:Apex trigger AutoConvert caused an unexpected exception, contact your administrator: AutoConvert: execution of AfterUpdate caused by: System.AssertException: Assertion Failed: Trigger.AutoConvert2: line 28, column 1
ShashankShashank (Salesforce Developers) 
Please check with removing the system.assert line and check if there is any issue?
Swami DeshanelSwami Deshanel
I am having another issue.  The trigger works great, except when the lead has an attachment.   Any ideas why that could be
ShashankShashank (Salesforce Developers) 
What exactly is the issue when there is an attachment?