Sunday, 26 October 2014

How to write an Apex trigger

Let’s write a trigger that’s both simple and practical!
We’ll write a trigger on the User object – all it will do is check the “Allow Forecasting” checkbox on every newly created user. Salesforce won’t let us modify this permission in a workflow, but the joke’s on them… we can just write a trigger!
trigger ForceForecasting on User (before insert) {
for (User userInLoop : Trigger.new) {
userInLoop.ForecastEnabled = true;
}
}
I added some color to the code to help you focus on what’s important. More importantly though, the colors will show you what you can ignore… save your brain power!
  • Green highlighted lines means it’s important! The deeper the green, the more useful the code. Skim the code without any highlights, that’s just boilerplate code.
  • Blue colored text are Salesforce reserved variables or sObjects. They have useful functionality that I will teach you to master!
  • Purple colored text are variables that we define. We can name them anything we want!
For now, try to guess what each word/line does based on its color.

No comments:

Post a Comment