+ Start a Discussion
SF_StevenSF_Steven 

LeadConvert on Lead Status Change to Qualified

I'm starting to get a little dangerous with APEX.  :smileyhappy:

 

My sandbox went down for mainteniance as I was getting ready to test this a second time - it didn;' work the first time.  I think I might be misunderstanding how trigger.old and new are suppsed to work.   Will this code automatically convert a lead when the status is changed to qualified?

 

Also - my test class only gets 76% converage.  To help get a better understanding, what would I need to add to get to 100%?

 

trigger LeadConvert on Lead (after update) {

  // no bulk processing; will only run from the UI and Lead Status must be changed to qualified 
  if (Trigger.new.size() == 1) {
    if (Trigger.old[0].isConverted == false)  {
    if (Trigger.new[0].isConverted == false && Trigger.new[0].Status == 'Qualified') {
 
              }
         }
    }
}

 Test Class:

 

public class LeadConvert {

    static testMethod void LeadConvert() {
    
    //create a lead with qualified status
    lead l1 = new lead();
    l1.status = 'Qualified';  
    l1.lastname = 'Joe';         
    l1.firstname = 'Smith';         
    l1.company = 'Company ABC'; 
    l1.isConverted = False ;
    insert l1;
    string holder = l1.id;
    
    //create lead that does not match criteria
    Lead L2 = new lead();
    L2.Status = 'Open';
    L2.lastname = 'Jane';         
    L2.firstname = 'Smith';         
    L2.company = 'Company DEF';
    L2.isConverted = False;
    insert L2;
    string holder2 = L2.id;  
    
    //Update 
    L2.Status = 'Qualified';
    update L2;
        }
 }

 

SF_StevenSF_Steven

Tried this too - no dice.  I thought this would work for sure too!! 

 

trigger LeadConvert on Lead (after update) {

  // no bulk processing; will only run from the UI and Lead Status must be qualified 
  if (Trigger.new.size() == 1) {
    if (Trigger.old[0].Status <> 'Qualified' && Trigger.new[0].Status == 'Qualified') {
 
             
         }
    }
}

 

JohanLiljegrenJohanLiljegren
You only have a couple of if statements in there, but nothing to actually convert the lead. Or did you leave out parts of the code?
You would need to call Lead.Convert() (or whatever it's callaed) in your inner if statement.
SF_StevenSF_Steven
public class LeadConvert {

    static testMethod void LeadConvert() {
    
    //create a lead with qualified status
    lead l1 = new lead();
    l1.status = 'Qualified';  
    l1.lastname = 'Joe';         
    l1.firstname = 'Smith';         
    l1.company = 'Company ABC'; 
    l1.isConverted = False ;
    insert l1;
    string holder = l1.id;
    
    //create lead that does not match criteria
    Lead L2 = new lead();
    L2.Status = 'Open';
    L2.lastname = 'Jane';         
    L2.firstname = 'Smith';         
    L2.company = 'Company DEF';
    L2.isConverted = False;
    insert L2;
    string holder2 = L2.id;  
    
    //Update 
    L2.Status = 'Qualified';
    update L2;
        }
 }

 

You're right - i forgot to paste the last part.  I got it working - the code is here:

 

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 == 'Qualified')) {
Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Qualified';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(true);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        }
        }
}

 Now on this test class - any idea how I can get this moved from 81% coverage to 100% coverage?

 

 

JohanLiljegrenJohanLiljegren
You seem to have changed your trigger quite a bit. Congrats on deciding to support bulk operations - it's almost always a good idea.
At a quick glance you should have 100% coverage with that test class, but I may be missing something.
Start by rerunning the test method and then look at exactly which rows are not covered. This is available when you click the "81%" part in the test output window - it pops up your class with red lines where it's not covered.
SF_StevenSF_Steven

Thanks for the reference - I didn't know I could just click on that number to visually see the coverage - that helps!

 

 

Now the last piece I'm struggling with is how to write the syntax so that the system checks to see if there is already a matching AccountID.   If so, then populate any blank fields and do not overwrite anything else (which I think is the default .  On the contact object, I want it to look at first name, last name and phone for the match.