[SalesForce] Roll up summary trigger for formula field

I have two object which are in lookup relationship Opportunity_c(Parent) and Recipt_c(child). And i have Due_Amount__c(formula) field in child object i.e
Due_Amount__c = Payment_Schedule_c – Received_c, now i want to roll up Due_Amount__c amount to "Due_as_on_date__c" field which is in parent object. Just because Due_Amount__c field is a formula field trigger is throwing error, i can't rollup can somebody help me out.
here is my rollup summary trigger.

trigger rollup_to_opp_from_recipt on Recipt__c ( after insert, after update) {

    list<string> oppId = new list <string>();

    for (Recipt__c rec : trigger.new ){
        if(rec.Opportunity__c != null && (trigger.isInsert ||  trigger.isUpdate|| trigger.isUnDelete || trigger.isDelete|| rec.Received__c != trigger.oldMap.get(rec.id).Received__c || rec.Due_Amount__c != trigger.oldMap.get(rec.id).Due_Amount__c ))oppId.add(rec.Opportunity__c);


    }

    if (oppId.size() > 0){

        map<id, Opportunity__c> Oppies = new map<id, Opportunity__c>([SELECT id FROM Opportunity__c WHERE id =: oppId]);

        for (Opportunity__c o : oppies.values())
        {
        o.Total_Received_Amount__c = 0;
        o.Due_as_on_Date__c = 0;
        }
        for (Recipt__c r : [SELECT Id, Received__c,Opportunity__c,Due__c,Due_Amount__c FROM Recipt__c WHERE Opportunity__c =: oppId]){
            Opportunity__c op = Oppies.get(r.Opportunity__c);
            op.Total_Received_Amount__c += r.Received__c;
            op.Due_as_on_Date__c += r.Due_Amount__c;
            update op;
        }
        upsert oppies.values();
    }
}

When i try to save record trigger throws "null.pointer exception" error please help me.

Best Answer

As you were getting a null pointer issue from this trigger, without knowing the line which threw this error, my guess is that you were inserting a Recipt__c object and the trigger.oldMap.get(rec.id).Received__c statement in your code would have thrown a null pointer exception. The oldMap variable is only available in update and delete triggers therefore when inserting a Receipt an exception was thrown. Documentation on trigger context variables can be found in the following link http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

To fix this issue you could implement something like the following

for (Recipt__c rec : trigger.new )
    {
        if(rec.Opportunity__c != null)
        {
            //if the trigger is insert there will be no trigger.oldMap
            //if inserting the object the Opp rollup will be required therefore add the Id to the list.
            if(trigger.isInsert)
            {
                oppId.add(rec.Opportunity__c);
            } //if updating a record check to see if the two values to rollup have changed, if so add Opp to the list
            else if (trigger.isUpdate && (rec.Received__c != trigger.oldMap.get(rec.id).Received__c || rec.Due_Amount__c != trigger.oldMap.get(rec.id).Due_Amount__c ))
            {
                oppId.add(rec.Opportunity__c);
            }
        }
    }

in place of

for (Recipt__c rec : trigger.new ){
    if(rec.Opportunity__c != null && (trigger.isInsert ||  trigger.isUpdate|| trigger.isUnDelete || trigger.isDelete|| rec.Received__c != trigger.oldMap.get(rec.id).Received__c || rec.Due_Amount__c != trigger.oldMap.get(rec.id).Due_Amount__c ))oppId.add(rec.Opportunity__c);


}
Related Topic