You need to sign in to do that
Don't have an account?
peterppt
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.
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:
I've done some gold plating as I could in all honestly leave you with a bad example!
All Answers
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:
I've done some gold plating as I could in all honestly leave you with a bad example!
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.