You need to sign in to do that
Don't have an account?
SFDC12
testclass for triggers
Hi everyone,below is the trigger and testclass which i have written.ablw to cover 70% can some one plz help me to increase the code coverage.
trigger:
trigger updateoppstage on Account (after update) {
set<id> accountId=new set<id>();
for(Account a:trigger.new){
accountId.add(a.id);
}
//query the account related opp
List<opportunity>opp=new list<opportunity>();
List<opportunity>opplist=[select id,name,stageName,createdDate,Accountid from opportunity where Accountid=:accountid];
for(opportunity o:opplist){
if(o.stageName!='ClosedWon'&& o.CreatedDate<system.today()){
o.stageName='ClosedLost';
}
opp.add(o);
}
update opp;
}
testclass:
@isTest
public class Testupdateoppstage {
@isTest
static void call(){
Account a=new Account();
a.name='testacc';
insert a;
opportunity o=new opportunity();
o.name='testopp';
o.StageName='needanalysis';
o.CloseDate=system.today();
//o.CreatedDate=2-2-2021;
o.AccountId=a.id;
Test.startTest();
List<opportunity>opp=[select id,accountId,stageName,name,closeDate,createdDate from opportunity where accountid=:a.id];
update a;
o.stageName='ClosedLost';
update opp;
//system.assertEquals(o.stageName, 'ClosedLost');
Test.stopTest();
}
}
Thanks in advance
trigger:
trigger updateoppstage on Account (after update) {
set<id> accountId=new set<id>();
for(Account a:trigger.new){
accountId.add(a.id);
}
//query the account related opp
List<opportunity>opp=new list<opportunity>();
List<opportunity>opplist=[select id,name,stageName,createdDate,Accountid from opportunity where Accountid=:accountid];
for(opportunity o:opplist){
if(o.stageName!='ClosedWon'&& o.CreatedDate<system.today()){
o.stageName='ClosedLost';
}
opp.add(o);
}
update opp;
}
testclass:
@isTest
public class Testupdateoppstage {
@isTest
static void call(){
Account a=new Account();
a.name='testacc';
insert a;
opportunity o=new opportunity();
o.name='testopp';
o.StageName='needanalysis';
o.CloseDate=system.today();
//o.CreatedDate=2-2-2021;
o.AccountId=a.id;
Test.startTest();
List<opportunity>opp=[select id,accountId,stageName,name,closeDate,createdDate from opportunity where accountid=:a.id];
update a;
o.stageName='ClosedLost';
update opp;
//system.assertEquals(o.stageName, 'ClosedLost');
Test.stopTest();
}
}
Thanks in advance
Try Below Code Plase Mark It As Best Answer If It Helps
Thank You!
All Answers
Try Below Code Plase Mark It As Best Answer If It Helps
Thank You!