Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Trigger on insert to set the value of one field to...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Trigger on insert to set the value of one field to that of another
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-14-2012 01:59 PM
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 nothingI'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.
Solved! Go to Solution.
Re: Trigger on insert to set the value of one field to that of another
[ Edited ]- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-14-2012 02:09 PM - edited 11-14-2012 02:10 PM
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!
Re: Trigger on insert to set the value of one field to that of another
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-15-2012 12:12 AM
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.

