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
sflearningsflearning 

Need help for code coverage!! urgent...

public class LeadCovertclass {
  
    @AuraEnabled
   Public lead lObj;
 
    @AuraEnabled
    Public Id leadId;
    
    @AuraEnabled
   Public Id oppId, accountId, contactId ;
   
    @AuraEnabled
    public Boolean showOpportunity { get; set; }
    
   @AuraEnabled
   public boolean isVisible {get;set;}

    @AuraEnabled
   public String Accountfounds {get; set;}
  
    @AuraEnabled
     public String oppName {get; set;}
    
    @AuraEnabled    
     public String accountName {get; set;}
    
    @AuraEnabled
     public String contactName {get; set;}
   
    
    private Account [] findCompany (string companyName) {
        
        //perform the SOSL query
        List<List<SObject>> searchList = [
            FIND :companyName 
            IN NAME FIELDS 
            RETURNING 
            Account(
                Id, 
                Name
            )
        ];
        
        List <Account> accountsFound = new List<Account>();
        
        for (List <sobject> sObjs : searchList) {
            
            for (sObject s : sObjs) {
              
              //add the account that was found to the list of found accounts
                accountsFound.add((Account) s);
            }   
        }
        
        // return the list of found accounts
        return accountsFound;
    }
   
    @AuraEnabled
    public static LeadCovertclass autoConvertRun(Id leadid)
    {
       
        LeadCovertclass obj = new LeadCovertclass();
        obj.isVisible = false;
       Lead l = [Select Id, Name, isConverted, Company From Lead where id= :leadid limit 1];
        
        if(l.isConverted == false){
        obj.isVisible = true;
      
        string company = l.Company;
        Account [] accountsFound = obj.findCompany(company + '*');
                
        
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(leadId);
        
        if (accountsFound != null && accountsFound.size() > 0) {          
           for (Account a : accountsFound) {
           lc.setAccountId(a.Id);
               
               AggregateResult[] oppcount = [SELECT COUNT(Id), AccountId FROM Opportunity where AccountId=:a.Id GROUP BY AccountId]; 
               if(oppcount.size() > 0){
               lc.setDoNotCreateOpportunity(true);
               obj.showOpportunity = false;
           }
           }
         }
        
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
                
        
        if (lcr.isSuccess()) {
            
             obj.oppId = lcr.getOpportunityId();
            if(obj.oppId != null){  
                obj.showOpportunity = true;     
                Opportunity opp = [SELECT Name from Opportunity where id =: obj.oppId];
                obj.oppName = opp.Name;
            }         
                    
        obj.accountId = lcr.getAccountId(); 
        obj.contactId = lcr.getContactId();        
        Account acc = [SELECT Name from Account where id =: obj.accountId];
        obj.accountName = acc.Name;
        Contact cn = [SELECT Name from Contact where id =: obj.contactId];
        obj.contactName = cn.Name;
             
        }
        return obj;       
    }
    else
    return null;
    } 
}

 
Best Answer chosen by sflearning
Raj VakatiRaj Vakati
@isTest(SeeAllData=true)
private class ConvertLeadTest {
    
    @isTest
 	static void test_convert_lead_opportunity() {
        
        Lead ld = new Lead(
            firstName = 'aksjdkasgdgas',
            lastName = 'asjkdkajgsdfgasfgf',
            company = 'Salesforce'
        );
        
        insert ld;
        
        System.Test.startTest();
        LeadCovertclass.autoConvertRun(ld.id);
        System.Test.stopTest();
        
    }
    
    @isTest
    static void test_convert_lead_no_opportunity() {
        
        Account acct = new Account(
            name = 'Salesforce '
        );
        
        insert acct;
        
        Contact cont = new Contact(
            accountId = acct.id,
            firstName = 'demooo',
            lastName = 'userrrrrrrrrrrr'
        );
        
        insert cont;
        
        Lead ld = new Lead(
            firstName = 'Test ',
            lastName = 'salesforce user',
            company = 'Salesforce'
        );
        
        insert ld;
        
        System.Test.startTest();
                LeadCovertclass.autoConvertRun(ld.id);

        System.Test.stopTest();
        
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
@isTest(SeeAllData=true)
private class ConvertLeadTest {
    
    @isTest
 	static void test_convert_lead_opportunity() {
        
        Lead ld = new Lead(
            firstName = 'aksjdkasgdgas',
            lastName = 'asjkdkajgsdfgasfgf',
            company = 'Salesforce'
        );
        
        insert ld;
        
        System.Test.startTest();
        LeadCovertclass.autoConvertRun(ld.id);
        System.Test.stopTest();
        
    }
    
    @isTest
    static void test_convert_lead_no_opportunity() {
        
        Account acct = new Account(
            name = 'Salesforce '
        );
        
        insert acct;
        
        Contact cont = new Contact(
            accountId = acct.id,
            firstName = 'demooo',
            lastName = 'userrrrrrrrrrrr'
        );
        
        insert cont;
        
        Lead ld = new Lead(
            firstName = 'Test ',
            lastName = 'salesforce user',
            company = 'Salesforce'
        );
        
        insert ld;
        
        System.Test.startTest();
                LeadCovertclass.autoConvertRun(ld.id);

        System.Test.stopTest();
        
    }
    
}

 
This was selected as the best answer
sflearningsflearning
Thanks a ton!!!! Amazing!!

only these line are not covering....
        if (accountsFound != null && accountsFound.size() > 0) {          
           for (Account a : accountsFound) {
           lc.setAccountId(a.Id);
               
               AggregateResult[] oppcount = [SELECT COUNT(Id), AccountId FROM Opportunity where AccountId=:a.Id GROUP BY AccountId]; 
               if(oppcount.size() > 0){
               lc.setDoNotCreateOpportunity(true);
               obj.showOpportunity = false;
           }
           }
         }
Raj VakatiRaj Vakati
Invoke findCompany method in test method 
Raj VakatiRaj Vakati
It will cover the code