You need to sign in to do that
Don't have an account?
Juul
Test class for lookup rollup
Hi,
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,
relationName,
formulaParent,
sobjectChild,
parentfield,
fieldChild;
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>) Trigger.new);
if(Trigger.isDelete)
objList = Trigger.old;
if(Trigger.isUpdate)
objList.addAll((List<sobject>) Trigger.old);
calculation.calculate(method, objList);
}
See website: http://blog.elieperez.net/salesforce-lookup-roll-up-summary/
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,
relationName,
formulaParent,
sobjectChild,
parentfield,
fieldChild;
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>) Trigger.new);
if(Trigger.isDelete)
objList = Trigger.old;
if(Trigger.isUpdate)
objList.addAll((List<sobject>) Trigger.old);
calculation.calculate(method, objList);
}
See website: http://blog.elieperez.net/salesforce-lookup-roll-up-summary/
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.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_example.htm
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm
http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
Regards,
Ashish