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
SeanCoSeanCo 

Test Unit Help for Custom APEX Roll-up Summary

Foreword:  First and foremost let me start by saying I am a *Newbie* to APEX and just getting my feet wet with more complex programming (I come from a web development background & in that sense VisualForce is much more on my level at this point).  I am not looking for someone to write the entire test unit code for me.  I do need help getting started and perhaps some very clear/basic explanation on how to get started.

 

Background:  We have a custom object (X360_Contract_Cycle__c) that is associated to cases by a lookup field (lookup field is on the case standard object).  The logic is when the Contract Name is the same as the value of the lookup field sum all case hours and populate a field on the custom object.  I was able to take code from the following article (http://colinloretz.com/2008/10/salesforce-rollup-summary-fields-using-apex-code/) and change the values to get it working in our Sandbox environment.  Here is my final code:

 

 

trigger rollupCaseHoursTo360 on Case (after delete, after insert, after update) {

double sumTotalHours = 0.0;
X360_Contract_Cycle__c [] sheetsToUpdate = new X360_Contract_Cycle__c[]{};


//***********************************************
//Updating new records
//***********************************************

if(Trigger.isInsert)
    {       
        
        Case [] teNew = trigger.new;

            for(Case te : teNew)
                {
                    
                    // 'X360_Contract_Cycle__c' that is part of the bind if the lookup field not custom object
                    for (X360_Contract_Cycle__c timesheet : [select Id, Name, Delivered_Hours__c from X360_Contract_Cycle__c where Id = :te.X360_Contract_Cycle__c])
                        {
                                //Sum all the timesheet entries
                                for (Case timeEntries: [select Id, Case_Hours__c from Case where Case_Hours__c != NULL and X360_Contract_Cycle__c = :timesheet.id])
                                    {
                                        //converting 'Case_Hours__c' from string data type to number (double)
                                        double caseHours = double.valueOf(timeEntries.Case_Hours__c);
                                        sumTotalHours += caseHours;
                                    }
                            
                            timesheet.Delivered_Hours__c = sumTotalHours;

                            //add timesheet to list to be updated outside of the loop
                            sheetsToUpdate.add(timesheet);
                        }
                }

//commit the changes to Salesforce
update sheetsToUpdate;
}


//***********************************************
//Code for updating when a record is deleted
//***********************************************

else if(Trigger.isDelete)
{
	Case [] teOld = trigger.old;

		for(Case te: teOld)
		{

			for (X360_Contract_Cycle__c timesheet: [select Id, Name, Delivered_Hours__c from X360_Contract_Cycle__c where Id = :te.X360_Contract_Cycle__c])
				{

                	for (Case timeEntries: [select Id, Case_Hours__c from Case where Case_Hours__c != NULL and X360_Contract_Cycle__c = :timesheet.id])
                  		{
                   			//converting 'Case_Hours__c' from string data type to number (double)
                    		double caseHours = double.valueOf(timeEntries.Case_Hours__c);
                    		sumTotalHours += caseHours;
                  		}

					timesheet.Delivered_Hours__c = sumTotalHours;
					sheetsToUpdate.add(timesheet);
				}
		}
		
update sheetsToUpdate;
}

//***********************************************
//Code for updating when a record is updated
//***********************************************

else if(Trigger.isUpdate)
{
//sum total both old and new
Case [] oldTime = Trigger.old;
Case [] newTime = Trigger.new;
Double newSum = 0.0;
Double oldSum = 0.0;

for(Case newTe: newTime)
{
for(Case oldTe : oldTime)
{

X360_Contract_Cycle__c oldTimesheet = [Select Id, Name, Delivered_Hours__c from X360_Contract_Cycle__c where Id = :oldTe.X360_Contract_Cycle__c];
X360_Contract_Cycle__c newTimesheet = [Select Id, Name, Delivered_Hours__c from X360_Contract_Cycle__c where Id = :newTe.X360_Contract_Cycle__c];

Case [] newSumHours = [Select Id, Case_Hours__c  from Case where X360_Contract_Cycle__c = :newTimesheet.Id];
Case [] oldSumHours = [Select Id, Case_Hours__c from Case where X360_Contract_Cycle__c = :oldTimesheet.Id];

//sum premiums from child objects
for(Case oldSumHour : oldSumHours)
{
	
	//converting 'Case_Hours__c' from string data type to number (double)
    double oldCaseHours = double.valueOf(oldSumHour.Case_Hours__c);
	oldSum += oldCaseHours;
}

for(Case newSumHour : newSumHours)
{
	
	//converting 'Case_Hours__c' from string data type to number (double)
    double newCaseHours = double.valueOf(newSumHour.Case_Hours__c);
	newSum += newCaseHours;
}

newTimesheet.Delivered_Hours__c = newSum;
oldTimesheet.Delivered_Hours__c = oldSum;

//sheetsToUpdate.add(newTimesheet);
//sheetsToUpdate.add(oldTimesheet);

sheetsToUpdate.add(newTimesheet);
if(newTimesheet.Id != oldTimesheet.Id){
sheetsToUpdate.add(oldTimesheet);
}

}
}


update sheetsToUpdate;
}



}

 

At this point I need to write a test unit with a fair amount of coverage to deploy the trigger to production.  I am really struggling trying to understand how to get started with the test unit.  If anyone can provide assistance on how to get started I would be very grateful .  Thanks in advance!

 

Best Answer chosen by Admin (Salesforce Developers) 
SeanCoSeanCo

Disregard...we were able to get a basic unit test in place in order to deploy.