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

Test class for lookup rollup


Can anybody help me with a test class for this trigger? I've asked the author of the trigger to send me the test class, but no response. And I'm not familiar with writing triggers / test classes. I've got it working in my sandbox, but I can't deploy it in my production without a test class.

public class LookupCalculation{
    public enum Method {COUNT, SUM, MIN, MAX, AVG}
    private string sobjectParent,
    public LookupCalculation(string mysobjectParent, string myrelationName, string myformulaParent,
                             string mysobjectChild, string myparentfield, string myfieldChild){
        sobjectParent = mysobjectParent;
        relationName = myrelationName;
        formulaParent = myformulaParent;
        sobjectChild = mysobjectChild;
        parentfield = myparentfield;
        fieldChild = myfieldChild;
    public void Calculate(Method calculation, List<sobject> childList){
        set<Id> parentIdSet = new set<Id>();
        for(sobject sobj : childList)
            parentIdSet.add((Id) sobj.get(parentfield));
        string soqlParent = 'select id, (select ' + fieldChild + ' from ' + relationName + ') from ' + sobjectParent + '';
        List<sobject> parentList = Database.query(soqlParent);
        for(sobject parent : parentList){
            List<sobject> children = parent.getSObjects(relationName);
            if(children == null)
                children = new List<sobject>();
            Decimal counter = (mustSum(calculation))? 0 : null;
            if(calculation == Method.COUNT)
                counter = children.size();
            for(sobject child : children){
                Decimal value = (Decimal) child.get(fieldChild);
                if(mustSum(calculation) && value != null)
                    counter += value;
                else if(calculation == Method.MIN && (counter == null || value < counter))
                    counter = value;
                else if(calculation == Method.MAX && (counter == null || value > counter))
                    counter = value;
            if(calculation == Method.AVG && children.size() > 0)
                counter = counter / children.size();
            parent.put(formulaParent, counter);
        update parentList;
    private boolean mustSum(Method calculation){
        return (calculation == Method.SUM || calculation == Method.AVG);


trigger InvoiceLineRollUp on Invoice_Line__c (after insert,after update,after delete,after undelete) {
    /*******************TO BE CUSTOMIZED*********************/
    string mysobjectParent = 'Invoice__c',      // Parent sobject API Name
           myrelationName = 'Invoice_Lines__r', // Api name of the relation between parent and child (ends with __r)
           myformulaParent = 'Total__c',        // Api name of the number field that will contain the calculation
           mysobjectChild = 'Invoice_Line__c',  // Child sobject API Name
           myparentfield = 'Parent_Invoice__c', // Api name of the lookup field on chield object
           myfieldChild = 'Amount__c';          // Api name of the child field to roll up
    LookupCalculation.Method method = LookupCalculation.Method.SUM; //Selected method: could be COUNT, SUM, MIN, MAX, AVG
    LookupCalculation calculation = new LookupCalculation(mysobjectParent, myrelationName, myformulaParent,
                                                          mysobjectChild, myparentfield, myfieldChild);
    List<sobject> objList = new List<sobject>((List<sobject>);
        objList = Trigger.old;
        objList.addAll((List<sobject>) Trigger.old);
    calculation.calculate(method, objList);

See website:
Hi Chris, 

Basically in the test class you have to enter dummy records that check if the functionality f the class is achieved or not. 

See the links below which ll help you understand and write the test class.

Thomas Peng - ArticulateThomas Peng - Articulate
I used the code to create a rollup on Assets.   Here's my test class very simple and gets me over 80% code coverage. 
public class TestAssetTrigger {

    static testMethod void testAddingAssets(){
        Account newAccount = new Account();
        newAccount.Name = 'Testing';
        insert newAccount;
        Asset newAssets = new Asset();
        newAssets.AccountId = newAccount.Id;
        newAssets.Name = 'Thomas';

            insert newAssets;        
            delete newAssets;