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
kavya T 5kavya T 5 

how to cover 100% code coverage for below trigger

Hi All,
   I am new to Salesforce and i tried test class for below trigger but i got code coverage only 35%.

whenever An Account is created with number_of_Employees__c(custom field)>99 create 10 opportunities for Account.

trigger CreateTenOpportunity on Account (after insert)
{
    List<Opportunity> oppList=new List<Opportunity>();
    for(Account acc:trigger.new)
    {
        if(acc.Number_of_Employees__c>99)
        {
            for(integer i=0;i<10;i++)
            {
              opportunity opp=new opportunity();
              opp.Name=acc.Name+ i;
              opp.AccountId=acc.Id;
              opp.StageName='Prospecting';
              opp.CloseDate=Date.today()+15;
              oppList.add(opp);  
            }
        }
    }
    if(oppList.size()!=0){
        insert oppList;
    }
}

 
Best Answer chosen by kavya T 5
Maharajan CMaharajan C
Hi Kavya,

In the Trigger you are using the custom Number_of_Employees__c Field but in the Test Class you reffered wrongly the Standard NumberOfEmployees Field from Account Object instead of cutome field to insert the Account  .  Change the test class like below it will work.

@isTest
private class CreateTenOpportunityTest {
    @isTest
    static void createOpportunityAfterAccount(){
        account acc=new account();
        acc.name='Test';
        acc.Number_of_Employees__c=100;           
        insert acc;
        
        List<Opportunity> opplist=[select name,AccountId from opportunity where AccountId=:acc.Id];
        system.assertEquals(10, opplist.size());
        
    }

}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Post your test class here and let me know what are the lines not covered in your above trigger.
kavya T 5kavya T 5
Hi Maharajan,
 below is my test class

@isTest
private class CreateTenOpportunityTest {
    @isTest
    static void createOpportunityAfterAccount(){
        account acc=new account();
        acc.name='Test';
        acc.NumberOfEmployees=100;
        insert acc;
        for(integer i=0;i<10;i++){
            opportunity opp=new Opportunity();
        opp.name=acc.Name+ i;
        opp.AccountId=acc.Id;
        opp.StageName='prospecting';
        opp.CloseDate=system.today()+15;
        insert opp;
        }
         
        List<Opportunity> opplist=[select name,opportunity.accountId from opportunity where AccountId=:acc.Id];
        system.assertEquals(10, opplist.size());
        
    }

}
kavya T 5kavya T 5
User-added image
Maharajan CMaharajan C
Hi Kavya,

In the Trigger you are using the custom Number_of_Employees__c Field but in the Test Class you reffered wrongly the Standard NumberOfEmployees Field from Account Object instead of cutome field to insert the Account  .  Change the test class like below it will work.

@isTest
private class CreateTenOpportunityTest {
    @isTest
    static void createOpportunityAfterAccount(){
        account acc=new account();
        acc.name='Test';
        acc.Number_of_Employees__c=100;           
        insert acc;
        
        List<Opportunity> opplist=[select name,AccountId from opportunity where AccountId=:acc.Id];
        system.assertEquals(10, opplist.size());
        
    }

}

Thanks,
Maharajan.C
This was selected as the best answer
kavya T 5kavya T 5
Thank you Maharajan.