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
vinni shreevinni shree 

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 
 

 

Best Answer chosen by vinni shree
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vinni,

Can you try the below test class which gives you 100% coverage for apex class and trigger.
 
@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';
        opp.CloseDate=date.today();
        opp.Name='firstopp';
        opp.amount=1000;
        insert opp;
        acc.name='sample';
        update acc;
      list<opportunity> ops =[select name,accountid, stagename from opportunity where accountid =: acc.Id];
        system.assertEquals('Closed Lost',ops[0].stagename );
        
          }

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

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,
 
vinni shreevinni shree

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

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vinni,

Can you check if below trigger is working for you.
 
trigger exa on Account (after update) {
  Example.invoke(Trigger.new);
}
 
public class Usecase10 {    
 public static void invoke(list <opportunity> opportunities){         
     for(opportunity opp:opportunities){
         opp.Amount=opp.amount-(opp.amount*0.1);
     }
     }
    public static void invoke1(list <opportunity> opportunities,map<id,opportunity> oldmap){         
     for(opportunity opp:opportunities){
         opportunity oldopp=oldmap.get(opp.id);
         if(opp.Amount!=oldopp.Amount)
         opp.Amount=opp.amount-(opp.amount*0.1);
     }
     }
}

Let me know if this works so I can share the test class for the same.

Thanks,
 
vinni shreevinni shree

Hi praveen,

code which you shared now is not related to the question i asked, you have shared me other scenario code

Thank you

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vinni,

Can you share me exact scenerio with some example so I can share the excat code.

Thanks,
 
vinni shreevinni shree

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 
 

Sai PraveenSai Praveen (Salesforce Developers) 
HI vinni,

How can an opportunity created date greater than today ? I dont think there is something missing here.

Thanks,
 
vinni shreevinni shree

Hi praveen,

Opportunity created date here means (Today - opportunity created date )> 30 days , sorry If I wasn't clear earlier about this 

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vinni,

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
public class Example{
    public static void invoke(list<Account> ac){
   // List<Account> accounts =[select id, name from Account];
   set<id> accountid= new set<id>();
        For(Account acc:ac){
          accountid.add(acc.id);  
        }
        List<Opportunity> opptoupdate= new List<Opportunity>();
        List<opportunity> opplist=[select id,Accountid,stagename,createddate from opportunity where accountid in:accountid and stagename !='Closed won' and stagename!='closed lost'  and  createddate=LAST_N_DAYS:30];
        for(Opportunity opp:opplist){
            opp.stagename='Closed Lost';
            opptoupdate.add(opp);
        }
        update opptoupdate;
}
}

If this trigger works for you I will share test class for the same.

The trigger which you shared had some issues.

Thanks,
 
vinni shreevinni shree
Thank you praveen, it is working
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vinni,

Do you need test class for the same?

Thanks,
 
vinni shreevinni shree

Yes praveen,

Thank you

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vinni,

Can you try the below test class which gives you 100% coverage for apex class and trigger.
 
@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';
        opp.CloseDate=date.today();
        opp.Name='firstopp';
        opp.amount=1000;
        insert opp;
        acc.name='sample';
        update acc;
      list<opportunity> ops =[select name,accountid, stagename from opportunity where accountid =: acc.Id];
        system.assertEquals('Closed Lost',ops[0].stagename );
        
          }

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
vinni shreevinni shree

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
 

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vinni,

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,
 
vinni shreevinni shree
okay praveen, Thanks a lot.