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
Chitral ChaddaChitral Chadda 

test class issue

trigger setAccountToProspect on Opportunity (after insert , after update) {

set<id> accId = new set<id>();
for(opportunity op :trigger.new)
 {
   if(op.AccountId!=null)
    {
     accId.add(op.AccountId);
    }
 }
 list<account> accMap = [ select Type from account where id IN : accId ];
 map<id,account> accMaps = new map<id,account>();
 for(account a :accMap)
 {
 accMaps.put(a.id,a);
 }
//     map<id,account> accMaps = new map<id,account>([select id, Type from account where id IN : accId ]);
list<account> ac = new list<account>();

for(opportunity o :trigger.new)
    {
      if(accMaps.get(o.accountid).Type=='Other')
           {
            if(o.StageName=='Prospecting' || o.StageName=='Qualification' || o.StageName=='Needs Analysis')
            {
             account a = accMaps.get(o.Accountid); 
              a.Type = 'Prospect';
              ac.add(a);
            }  
           } 
     }
     update ac;      
}
hello whenever from opportunity if stage name is prospecting,qualification etc
ur parent account type is set to prospect then.
 
@isTest
public class testsetAccountToProspect
{
static testMethod void updateaccount()
{

 account acct = new account();
 acct.name='ABC';
 acct.Type='Other';
 insert acct;
  
  opportunity oppt = new opportunity(Accountid=acct.id);
  oppt.StageName='Prospecting';
  oppt.Name='Test';
  oppt.closeDate = date.today() + 30;
  insert oppt;
  
  
  acct = [ select id,type from account where id=: acct.id];
  system.assertequals('Prospect',acct.Type);
 }
 }

help with test class 
Best Answer chosen by Chitral Chadda
PratikPratik (Salesforce Developers) 
Hi Chitral,

I have modified the trigger and test class. Please check this:


Trigger:

trigger oppaccupdt on opportunity(after insert, after update) {
    
    set<id> accids=new set<id>();
    list<account> acclist= new list<account> ();
    list<account> accupdate= new list<account> ();
    map<id,account> accmap= new map<id,account>();
    
    
    for(opportunity opp:trigger.new) {
        
        accids.add(opp.accountid);
    }
    
    acclist=[select id,type from account where id=:accids];
    
    for(account a:acclist) {
    accmap.put(a.id,a);
    
    }
   
    for(opportunity oppty:trigger.new) {
    
        If(oppty.stagename=='Prospecting' || oppty.stagename=='Qualification') {
            If(accmap.get(oppty.accountid).type=='Other') {
           account acc= accmap.get(oppty.accountid);
           acc.type='Prospect';
           accupdate.add(acc);
           
           }
        
        }
    
    } 
    
    If(accupdate.size() > 0)
    update accupdate;
    
}



TestClass:


@istest

Public class testopptrgr {

    private static testmethod void testmethodopp() {
    
     account acct = new account();
     acct.name='ABC123';
     acct.Type='Other';
     insert acct;
      
     id accid=acct.id;
      
      opportunity oppt = new opportunity();
      oppt.accountid=accid;
      oppt.StageName='Prospecting';
      oppt.Name='Test';
      oppt.closeDate = date.today() + 30;
      insert oppt;
      
      
      acct = [ select id,type from account where id=: acct.id];
      system.assertequals('Prospect',acct.Type);
        
    }

}



It gave me 100% code coverage.Try this and let me know how it goes.


Thanks,
Pratik

All Answers

PratikPratik (Salesforce Developers) 
Hi Chitral,

You can update the opportunity to stage Qualification and Need analysis aso the OR part will be covered.

What issue you are cusarrently facinf in this test class?

Thanks,
Pratik 
PratikPratik (Salesforce Developers) 
Hi Chitral:

Here is the test class for 100% coverage:

Test Class:

@isTest
public class testsetAccountToProspect
{
static testMethod void updateaccount()
{

 account acct = new account();
 acct.name='ABC';
 acct.Type='Other';
 insert acct;
  
  opportunity oppt = new opportunity(Accountid=acct.id);
  oppt.StageName='Prospecting';
  oppt.Name='Test';
  oppt.closeDate = date.today() + 30;
  insert oppt;
  
  
  acct = [ select id,type from account where id=: acct.id];
  system.assertequals('Prospect',acct.Type);
  
  oppt.stagename='Needs Analysis';
  update oppt;
  
  oppt.stagename='Qualification';
  update oppt;
  
 }
 }


I tried and it return 100% test coverage for the trigger.

Thanks,
Pratik


P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.
Chitral ChaddaChitral Chadda
that what i did 
again i copy pasted ur code nd got this error
System.AssertException: Assertion Failed: Expected: Prospect, Actual: Other
PratikPratik (Salesforce Developers) 
Hi Chitral,

Can you please try it by removing the System.assertequals statement and then rune the test class.
The System.assertequals statement is giving the different result than the expected "Prospect" value.

Thanks,
Pratik
Chitral ChaddaChitral Chadda
test method :pass
but its not showing code coverage still
0%
PratikPratik (Salesforce Developers) 
Hi Chitral,

I have modified the trigger and test class. Please check this:


Trigger:

trigger oppaccupdt on opportunity(after insert, after update) {
    
    set<id> accids=new set<id>();
    list<account> acclist= new list<account> ();
    list<account> accupdate= new list<account> ();
    map<id,account> accmap= new map<id,account>();
    
    
    for(opportunity opp:trigger.new) {
        
        accids.add(opp.accountid);
    }
    
    acclist=[select id,type from account where id=:accids];
    
    for(account a:acclist) {
    accmap.put(a.id,a);
    
    }
   
    for(opportunity oppty:trigger.new) {
    
        If(oppty.stagename=='Prospecting' || oppty.stagename=='Qualification') {
            If(accmap.get(oppty.accountid).type=='Other') {
           account acc= accmap.get(oppty.accountid);
           acc.type='Prospect';
           accupdate.add(acc);
           
           }
        
        }
    
    } 
    
    If(accupdate.size() > 0)
    update accupdate;
    
}



TestClass:


@istest

Public class testopptrgr {

    private static testmethod void testmethodopp() {
    
     account acct = new account();
     acct.name='ABC123';
     acct.Type='Other';
     insert acct;
      
     id accid=acct.id;
      
      opportunity oppt = new opportunity();
      oppt.accountid=accid;
      oppt.StageName='Prospecting';
      oppt.Name='Test';
      oppt.closeDate = date.today() + 30;
      insert oppt;
      
      
      acct = [ select id,type from account where id=: acct.id];
      system.assertequals('Prospect',acct.Type);
        
    }

}



It gave me 100% code coverage.Try this and let me know how it goes.


Thanks,
Pratik
This was selected as the best answer
Chitral ChaddaChitral Chadda

thnkyou dear...
it works fine with ur code

bt strange why its nt showing any coverage in my code ..
 

PratikPratik (Salesforce Developers) 
Hi Chitral,

Glad it helped!

You can check for any conflicts with your trigger i.e. any other trigger or workflow may be changing the stage value.

Thanks,
Pratik