1

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.

2

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);


}
2

After implementing your trigger and objects (well I just used Opportunity, rather than a new custom object ;D ), I found that null pointer exceptions were only generated when the value of the Due_Amount__c formula field was blank, and the blank field handling on the formula field edit page was set to "Treat blank fields as blanks" rather than "Treat blank fields as zeroes" under the advanced formula options.

If this setting is set to "Treat blank fields as zeroes" (see picture below), the null pointer error disappears, so I would recommend you check this value in your org.

enter image description here

A couple of bonus pointers when writing this kind of trigger:

  1. You have a DML update operation inside a for loop, this is a really inefficient (and potentially system breaking) way of updating your records. Modify all the opportunity values as appropriate, then update them all in one go.
  2. Your trigger is only fired after update and after insert, yet your condition checking in the first if statement checks for isUndelete and isDelete. See Michael Flemings answer for a good example of how to re-factor your if conditions correctly.

Hope that helps, CAL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.