0

I am trying to increment a field on a parent object Deal__c with every child object Deliverable__c created. My debug shows an id for theDeal but the theDeal.DeliverablesIncrementer__c+=1; is getting an "attempt to reference a null object" error I also want to use the same incremented number on the deliverable itself.

trigger DeliverableDealIncrementer on Deliverable__c (after insert) {

    for (Deliverable__c DD : system.trigger.new){
        Deal__c theDeal = [select id,   DeliverablesIncrementer__c from Deal__c where id=: DD.deal__c];
        system.debug('thedeal======================'+theDeal);


        theDeal.DeliverablesIncrementer__c+=1;
        DD.Deal_Deliverable_Decimal_del__c +=1;
    database.update(theDeal,false);

    } 

}
| improve this question | | | | |
2

The problem is that DeliverablesIncrementer__c on the record itself is null, that's why `

system.debug('thedeal======================'+theDeal);`

shows you only Id. You need to modify your code to check if DeliverablesIncrementer__c is null and if so, just assign it to 1:

theDeal.DeliverablesIncrementer__c = (theDeal.DeliverablesIncrementer__c == null) ? 1 : theDeal.DeliverablesIncrementer__c + 1;
| improve this answer | | | | |
  • Thank you. I added a line to fix the field on the deliverable__c as well. Was not familiar with this notation. I wonder though - is this trigger configured for bulk input, and can it be with the incrementer? – jaw999 Dec 16 '16 at 13:41
  • The trigger in the original post is not configured for bulk, since you're performing a DML and a SOQL operation for each record, it'll be out of limits if you'll try to insert more than 100 records. – EvAzi Dec 16 '16 at 19:10

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.