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
brijender singh rathore 16brijender singh rathore 16 

TEST CLASS FOR ROLLUP

latest__c is rollup summary field = max(opportunity).This trigger is mapping latest opportunity amount into account field (acc.opty_amount__c).
trigger latest on Account (after update)  {    
  if(checkRecursive.runOnce())  
  {    set<id> acctid =new set<id>();    
  list<account> accnt=new list<account>();  
  for(account acc:trigger.new)  
  {       acctid.add(acc.id);           }          
  for(account acc : [select id,name,opty_amount__c,latest__c(select id,amount,CreatedDate from opportunities) from account where id  in:acctid])    {    list <opportunity> oppo = acc.opportunities;    
for(opportunity op:oppo)   
 {    if(acc.latest__c == op.CreatedDate)    
      {    acc.opty_amount__c=op.amount;  
      }
 
  }          
    accnt.add(acc);    
  }    
 update accnt;  
}
THIS TEST CLASS IS GIVING 80% COVERAGE PLZ HELP ME TO CONVERT IT TO 100%.
@isTest
private class UpdateAccount_Test
{  public static testMethod void TestUpdateAccount() 
  {        Account acc = new Account();           
         acc.Name = 'Test';              
         insert acc;         
         opportunity oppo = new opportunity();          
         oppo.accountid  =acc.id;     
         oppo.amount = 50000.00;                        
        update acc;    
        }  
 }
v varaprasadv varaprasad
Hi Bijender,

Please check once following code.
@isTest
private class UpdateAccount_Test
{  public static testMethod void TestUpdateAccount() 
  {      Account acc = new Account();           
         acc.Name = 'Test';              
         insert acc; 
		 
         opportunity oppo = new opportunity();          
         oppo.accountid  =acc.id;     
         oppo.amount = 50000.00; 
         insert oppo;

		acc.latest__c = system.today(); 
        update acc;    
        }  
 }
Hope this helps you!

Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com
 
brijender singh rathore 16brijender singh rathore 16
showing this error
Error: Compile Error: Field is not writeable: Account.latest__c at line 13 column 13

 
Niket SFNiket SF
- Account.latest__c is roolup as you mentioned, we can not define that, After your execution you do SOQL quesry on same record request for  latest__c field and check.

* note you need to do SOQL to get the latest__c field.
brijender singh rathore 16brijender singh rathore 16
DONE IT GUYS THANKS FOR THE RESPONSES
THIS ONE IS GIVING 100% COVERAGE

***I THINK DOING  SOQL  IN TEST CLASS IS CONSIDERED A BAD PRACTICE.  (PLZ CORRECT ME IF I AM WRONG)   *****

@isTest
private class UpdateAccount_Test{
 public static testMethod void TestUpdateAccount() {
 
     Account acc = new Account();
   
       acc.Name = 'Test';
    
    
     insert acc;
     
    opportunity oppo = new opportunity();
    oppo.name = 'bsr';
    oppo.amount = 50000.00;
    oppo.StageName = 'Closed Won';
    oppo.CloseDate = system.today();
    oppo.accountid  =acc.id;
    insert oppo;
    
    update acc;
   }
 
 }