You need to sign in to do that
Don't have an account?
kavya 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;
}
}
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;
}
}
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
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());
}
}
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