You need to sign in to do that
Don't have an account?
How to check for the opportunities in test class
Hi i have written code, this is a trigger which executes whenever the Account is updated check all opportunities related to the account. Update all Opportunities Stage to close lost if an opportunity created date is greater than 30 days from today and stage not equal to close won.
I got the logic right but didn't undersatnd how to write test class for this scenario. Please help on this. Thanks in advance
Apex class
public class Example{
public static void invoke(list<Account> ac){
List<Account> accounts =[select id, name from Account];
list<opportunity> opptoup = new list<opportunity>();
for(Account a:[select Id, name,(select Id, name,stagename from opportunities) from Account where Id IN:accounts]){
for(opportunity op:a.opportunities){
opptoup.add(op);
}
}
for(opportunity rec: opptoup){
if(rec.stagename!='closed won' && rec.stagename!='closed lost' && rec.CreatedDate>system.NOW()+30){
rec.stagename='closed won';
}
}
update opptoup;
}
}
Trigger
trigger exa on Account (after update) {
Example.invoke(Trigger.new);
}
Test class
@istest
public class useCase7Test {
@istest
static void testMe(){
Account acc= new Account();
acc.Name='doremon';
acc.phone='1234';
insert acc;
Opportunity opp= new opportunity();
opp.AccountId=acc.Id;
opp.StageName='prospecting';
Datetime mon=datetime.now().addDays(-30);
test.setCreatedDate(opp.Id,mon);
opp.CloseDate=date.newInstance(2022, 11, 3);
opp.Name='firstopp';
insert opp;
opportunity opp1= new opportunity();
opp1.AccountId=acc.Id;
opp1.StageName='closed lost';
Datetime mon1=datetime.now().addDays(-30);
test.setCreatedDate(opp1.Id,mon);
opp1.CloseDate=date.newInstance(2022,12, 31);
opp1.Name='secondopp';
insert opp1;
list<opportunity> ops =[select name, stagename from opportunity where accountid =: acc.Id];
system.debug(ops);
}
}
I wrote test class till here, able to fetch the opportunities but not able to check with the assertequals method
Can you try the below test class which gives you 100% coverage for apex class and trigger.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
Can you confirm how can be created date greater than 30 days to todays date. Are you creating the data from some other system and I don't think this is possible. Can you confirm on it so I can share the test class for the same.
Is the trigger working as expected. I guess there is some issue in the trigger logic as well.
Thanks,
Hi praveen,
O'm checking whether an opportunity is older than 30 days from today as of now i'm testing this on my existing records which has created date as june, july and october
Thank you
Can you check if below trigger is working for you.
Let me know if this works so I can share the test class for the same.
Thanks,
Hi praveen,
code which you shared now is not related to the question i asked, you have shared me other scenario code
Thank you
Can you share me exact scenerio with some example so I can share the excat code.
Thanks,
Yes praveen,
How to check for the opportunities in test class
Hi i have written code, this is a trigger which executes whenever the Account is updated check all opportunities related to the account. Update all Opportunities Stage to close lost if an opportunity created date is greater than 30 days from today and stage not equal to close won.
I got the logic right but didn't undersatnd how to write test class for this scenario. Please help on this. Thanks in advance.
Apex class
public class Example{
public static void invoke(list<Account> ac){
List<Account> accounts =[select id, name from Account];
list<opportunity> opptoup = new list<opportunity>();
for(Account a:[select Id, name,(select Id, name,stagename from opportunities) from Account where Id IN:accounts]){
for(opportunity op:a.opportunities){
opptoup.add(op);
}
}
for(opportunity rec: opptoup){
if(rec.stagename!='closed won' && rec.stagename!='closed lost' && rec.CreatedDate>system.NOW()+30){
rec.stagename='closed won';
}
}
update opptoup;
}
}
Trigger
trigger exa on Account (after update) {
Example.invoke(Trigger.new);
}
Test class
@istest
public class useCase7Test {
@istest
static void testMe(){
Account acc= new Account();
acc.Name='doremon';
acc.phone='1234';
insert acc;
Opportunity opp= new opportunity();
opp.AccountId=acc.Id;
opp.StageName='prospecting';
Datetime mon=datetime.now().addDays(-30);
test.setCreatedDate(opp.Id,mon);
opp.CloseDate=date.newInstance(2022, 11, 3);
opp.Name='firstopp';
insert opp;
opportunity opp1= new opportunity();
opp1.AccountId=acc.Id;
opp1.StageName='closed lost';
Datetime mon1=datetime.now().addDays(-30);
test.setCreatedDate(opp1.Id,mon);
opp1.CloseDate=date.newInstance(2022,12, 31);
opp1.Name='secondopp';
insert opp1;
list<opportunity> ops =[select name, stagename from opportunity where accountid =: acc.Id];
system.debug(ops);
}
}
I wrote test class till here, able to fetch the opportunities but not able to check with the assertequals method
How can an opportunity created date greater than today ? I dont think there is something missing here.
Thanks,
Hi praveen,
Opportunity created date here means (Today - opportunity created date )> 30 days , sorry If I wasn't clear earlier about this
My bad that I have pasted some other code in my above comment. Can you try the below .
I guess the stage of the opportunity should be Closed Lost . If closed won change it to closed won in highlited code and try it
If this trigger works for you I will share test class for the same.
The trigger which you shared had some issues.
Thanks,
Do you need test class for the same?
Thanks,
Yes praveen,
Thank you
Can you try the below test class which gives you 100% coverage for apex class and trigger.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
Hi praveen,
This worked but I have one doubt, in the actual code we are checking for created date condition as well but why didn't we include it while writing in the test class also why did you make closed date as today.
Thank you
We can keep close date as our wish . It can be Today()-1 or any date field.
Coming to created date it should be in last 30 days and our created opportunity is today so it worked here.
Thanks,