function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
peterpptpeterppt 

Trigger on insert to set the value of one field to that of another

I have the following code  which compiles but gives the following error:

 

Apex trigger Peter1 caused an unexpected exception, contact your administrator:
Peter1: execution of BeforeInsert caused by: System.NullPointerException:
Attempt to de-reference a null object: Trigger.Peter1: line 3, column 1

 

trigger Peter1 on Event (before insert) {

Event myEvent = trigger.old[0];
Event myNewEvent = trigger.new[0];
if (myEvent.IsAllDayEvent = True) {
myNewEvent.Subject = myEvent.Location; 
update myEvent;
}// else nothing

 I've had little experience with SF triggers to date and I'm struggling a bit with trigger.old and trigger.new !   Any help here would be much appreciated.  Thanks.

 

Best Answer chosen by Admin (Salesforce Developers) 
EnthEnth

Hi Peter

 

You can't reference the trigger.old context when you're doing an insert, only during an update or delete operation. You also must not call the update DML in the trigger as all data will be committed by salesforce after the trigger completes. If you want to simply copy the Subject to match the location all you would need is:

trigger Peter1 on Event (before insert) {

   // Check we're doing a before insert, whilst that's always true now if
   // someone else adds another event to this trigger we don't want it to
   // break!
   if (trigger.isBefore && trigger.isInsert) {
      // We bulkify the trigger so it works for n records being inserted
      for (Event e : trigger.new) {
         if (e.IsAllDayEvent) {
            e.Subject = e.Location;
         }
      }
   }
   // All done!
}


 I've done some gold plating as I could in all honestly leave you with a bad example!

 

All Answers

EnthEnth

Hi Peter

 

You can't reference the trigger.old context when you're doing an insert, only during an update or delete operation. You also must not call the update DML in the trigger as all data will be committed by salesforce after the trigger completes. If you want to simply copy the Subject to match the location all you would need is:

trigger Peter1 on Event (before insert) {

   // Check we're doing a before insert, whilst that's always true now if
   // someone else adds another event to this trigger we don't want it to
   // break!
   if (trigger.isBefore && trigger.isInsert) {
      // We bulkify the trigger so it works for n records being inserted
      for (Event e : trigger.new) {
         if (e.IsAllDayEvent) {
            e.Subject = e.Location;
         }
      }
   }
   // All done!
}


 I've done some gold plating as I could in all honestly leave you with a bad example!

 

This was selected as the best answer
peterpptpeterppt

Thanks Enth - not only for solving my problem but for explaining where I was going wrong.  I'm beginning to see how compact trigger code can be and also how powerful triggers are.  Thanks again.