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
GanuGanu 

rollupTrigger

Hi would anyone happen to know how to write a testmethod for my trigger below:

trigger rollupTrigger on Commitment_Draw__c (after insert, after update, after delete)
{
    double totalSum = 0;
    Commitment_Draw__c[] c = trigger.new;
    if(Trigger.isUpdate || Trigger.isInsert)
    {
       for(Commitment_Draw__c cd : c)
       {
        for(Opportunity parent : [select Id,Name from Opportunity where Id = :cd.Opportunity__c])
        {
         for(Commitment_Draw__c childObject : [select Id,Opportunity__c,Amount__c from Commitment_Draw__c where Opportunity__c = :parent.Id])
            {
                  totalSum += childObject.Amount__c;
            }
            parent.Rollup__c = totalSum;  
            update parent;                    
        }
      }
    }
    else if(Trigger.isDelete)
    {
        Commitment_Draw__c [] cold = Trigger.old;
      for(Commitment_Draw__c oldobj :cold) 
      {
        for (Opportunity parentObject : [select Id,Name from Opportunity where Id= :oldobj.Opportunity__c])
        {
          for (Commitment_Draw__c childObject : [select Id,Opportunity__c,Amount__c from Commitment_Draw__c where Opportunity__c = :parentObject.Id])  
          {
             totalSum += childObject.Amount__c; 
          }
            parentObject.Rollup__c = totalSum;  
            update parentObject;                     
         }  
      }
    }
}

mikefmikef
Ganu:

Write a functional testMethod and that will give you full coverage.

You want to write code that inserts your Commitment_Draw__c objects.
Make sure you create objects that are going to have values that call the different if statements on your trigger,
to get full coverage.

And one more thing, please test bulk inserts of more then 20 records.
You have queries inside for loops and you will hit query limits.
You will need to bulkafy your trigger.

Take a look at this post, http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=342#M342

Message Edited by mikef on 09-05-2008 12:00 PM
GanuGanu

Hi mikef,

Thanks for your reply,

Iam new to this Apex, so can u give me some sample code for test method.

 

 

mikefmikef
I would like to point you to the Apex docs for that, there are lots of examples in the docs.