0

I'm having problem with my trigger.Please help me to resolve this system null pointer exception

trigger theatreupdate on Transaction__c (before insert) 
{

    for(Transaction__c t:Trigger.new)
    {

        t.TheaterT__r.Number_Of_Seats_Sold__c=0;
    }
//TheaterT__r-parent of Transaction__c


}
1

2 Answers 2

1

You cannot update parent fields directly. Here is how to do it

trigger theatreupdate on Transaction__c (before insert) 
{
    Set<String> parentIds = new Set<String>();
    for(Transaction__c t:Trigger.new)
    {
       parentIDs.add(t.TheaterT__c);
    }

    List<Theater__c> theatres = [select Number_Of_Seats_Sold__c from Theater__c where id = :parentIds];

    for (TheaterT__c th : theatres_ }
       th.Number_Of_Seats_Sold__c=0;
    }

    update theatres;

}
1
  • Note to @Remya that in general, these kinds of things work better in after triggers as often, the object you want to insert or update may need the id of the triggered object - and the ID is not available until the 'after insert trigger. Not true in your specific use case but good to remember.
    – cropredy
    Commented Dec 6, 2015 at 21:51
0

You can not update parent fields directly in trigger. You have to create new instance and then do update. Only current object level fields can be updated.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .