• Laurent Jantzen
  • NEWBIE
  • 0 Points
  • Member since 2015
  • Responsable CRM
  • Association Docteur Souris


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Here is my VF page for lead conversion. not able to figure out what else needs in my test class to increase the coverage. PLease help.

<apex:page standardController="Lead" extensions="XXCSRLeadConvertClass"   title="Convert Lead" id="pgConvertLead">

<apex:form id="frmConvert">

    <apex:actionFunction name="afOpportunity" action="{!doNothing}" rerender="pbsConvert" immediate="true" />

    <apex:pageBlock title="Convert Lead" mode="edit">
   
        <apex:pageBlockButtons >
            <apex:commandButton id="cmdConvert" action="{!convertLead}" value="Convert" />
            <apex:commandButton id="cmdCancel" action="{!cancel}" value="Cancel" />
        </apex:pageBlockButtons>
       
        <apex:messages ></apex:messages>
       
        <apex:pageBlockSection id="pbsConvert" title="Convert Lead" columns="1">
                       
            <apex:inputField id="ifOwnerId" value="{!ldSource.OwnerId}" />
            <apex:selectList id="soAccount" value="{!strAccountId}" label="Account Name" size="1">
                <apex:selectOptions value="{!lstCompanyInfo}" />
            </apex:selectList>
            <apex:selectList id="soContact" value="{!strContactId}" label="Contact Name" size="1">
                <apex:selectOptions value="{!lstContactInfo}" />
            </apex:selectList>
                      
        </apex:pageBlockSection>
   
    </apex:pageBlock>

</apex:form>

</apex:page>

Here is extension class

public class XXCSRLeadConvertClass {
public Lead ldSource {get;set;}
public Boolean bolCreateOpp {get;set;}
public String strAccountId {get;set;}
public String strContactId {get;set;}



//////////////////////////
// Constructors / GETers
//////////////////////////
public XXCSRLeadConvertClass(ApexPages.StandardController scMain) {



ldSource = [SELECT Id, FirstName, LastName, OwnerId, Company, Street, City, State, PostalCOde, Country, Phone, Fax,Status,ApprovalStatus__c FROM Lead WHERE Id = :scMain.getId()];
bolCreateOpp = false;

    
}

public List<SelectOption> getlstCompanyInfo() {

String strCompanyWildcard = '%' + ldSource.Company + '%';
List<Account> lstAcct = [SELECT Id, Name, Owner.Name FROM Account WHERE Name LIKE :strCompanyWildcard];

List<SelectOption> lstCompanies = new List<SelectOption>();

// Add New Account if not found
lstCompanies.add(new SelectOption('1','Create New Account: ' + ldSource.Company));

// Add found Accounts to SelectList
for(Account a : lstAcct) {
lstCompanies.add(new SelectOption(a.Id, 'Attach to Existing: ' + a.Name + ' (' + a.Owner.Name + ')'));
}

return lstCompanies;      
}

    public List<SelectOption> getlstContactInfo(){
       
       string strContactFirst = '%'+ldSource.FirstName+'%';
        string strContactLast = '%'+ldSource.LastName+'%';
        List<Contact> lsContact = [select Id ,FirstName,LastName,AccountId,Owner.Name from Contact where (FirstName like : strContactFirst and LastName like :strContactLast)
                                   and AccountId = :[select Id from Account where Name like :ldSource.Company]];
       List<SelectOption> lstContact = new List<SelectOption>();
       
        // Add New Contact if not found
lstContact.add(new SelectOption('1','Create New Contact: ' + ldSource.FirstName+''+ldSource.LastName));

// Add found Accounts to SelectList
for(Contact c : lsContact) {
lstContact.add(new SelectOption(c.Id, 'Attach to Existing: ' + c.FirstName +' '+c.LastName+ ' (' + c.Owner.Name + ')'));
    }
return lstContact;         
    }
//////////////////////////
// Action Methods
//////////////////////////

public void doNothing() {  }

public PageReference convertLead() {

//check if status = qualified and Approved
if (ldSource.Status == 'Qualified' && ldSource.ApprovalStatus__c == 'Approved'){


// Create LeadConvert object
Database.LeadConvert lc = new Database.LeadConvert();

        
lc.setLeadId(ldSource.Id);
lc.setOwnerId(ldSource.OwnerId);
if(strAccountId.length() > 1) { lc.setAccountId(strAccountId); }
if(strContactId.length() > 1) { lc.setContactId(strContactId); }
lc.setDoNotCreateOpportunity(bolCreateOpp);

// Set Opportunity Name
if(bolCreateOpp == true) { lc.setOpportunityName(ldSource.Company); }

// Set Lead Converted Status
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);



// Convert!
Database.LeadConvertResult lcr = Database.convertLead(lc);

// Mop up Opportunity
if(bolCreateOpp == true) {
Opportunity o = new Opportunity(Id=lcr.getOpportunityId());
update o;
}
// Redirect...
PageReference prResult;
if(lcr.isSuccess()) {
prResult = new PageReference('/' + lcr.getAccountId());
prResult.setRedirect(true);
return prResult;  
} else {
return null;
}
}else{
ldSource.addError('Lead cannot be converted without Lead status Qualified and Approved !'); 
      ldSource.addError('Please Go back to Lead screen to change status'); 
    }
   
return null;}
}


Here is my test code

@IsTest(seealldata=true)
private class TestXXCSRLeadConvertClass{

        static testMethod void TestXXCSRLeadConvertClass() {
      
       test.startTest();
           Boolean bolCreateOpp;
           String strAccountId;
     String strContactId;
            Lead l = new Lead();
            l.FirstName = 'CRM Testing First';
            l.LastName = 'CRM Testing Last';
            l.Company = 'CRM Testing INCtest';
            l.description = 'Test descr';
            l.city = 'test';
            l.street = 'test';
            l.state = 'CA';
            l.country = 'United States';
            l.status = 'Qualified';
            l.email = 'test@testnetgear.com';
            l.website = 'www.testcrm.com';
            l.ApprovalStatus__c='Approved';
            l.RecordTypeId='012110000004b9Q';
             
        
            insert l;
       
      Id leadId = l.Id;
  bolCreateOpp = false;
  //Create a reference to the VF page
  PageReference pageRef = Page.XXCSR_LEAD_CONVERT;
        Test.setCurrentPageReference(pageRef);

     //Create an instance of the controller extension and call the autoRun method.
  //autoRun must be called explicitly even though it is "autoRun".
  ApexPages.StandardController sc = new ApexPages.standardController(l);
  XXCSRLeadConvertClass leadconvt = new XXCSRLeadConvertClass(sc);
   String nextPage = sc.save().getUrl();
 
      
       List<SelectOption> testacct = new List<SelectOption>();
       testacct = leadconvt.getlstCompanyInfo();
        system.debug(testacct);
    
       
  List<SelectOption> testcon = new List<SelectOption>();
       testcon = leadconvt.getlstContactInfo();
        system.debug(testcon);
 
   leadconvt.doNothing();
 
             //Retrieve the converted Lead info and the opps and roles.
   l = [select Id, IsConverted, ConvertedAccountId, ConvertedContactId ,Status, ApprovalStatus__c,
    Company,OwnerId,firstname,lastname,city,country from Lead where Id = :leadId];
         
          system.assert(!l.IsConverted,'Lead Converted' ); 
          test.stopTest();
        


        }
  
        

}

Thanks
Pallavi