-1

I can not able to understand myAccountTrigger provided on this link please help me through this Below is the Link

https://developer.salesforce.com/docs/atlas.en-us.224.0.apexcode.meta/apexcode/apex_triggers_context_variables.htm

  • What do you not understand? What this trigger is demonstrating? In terms of context variables or the fake business logic they're showing (which checks are best implemented in which trigger events) – eyescream Apr 24 '21 at 17:00
  • I can't able to understand what this trigger is demonstrating in terms of context variable – Kashish Bansal Apr 26 '21 at 09:13

1 Answers1

0

If you're familiar with other programming languages or databases then perhaps start with self-paced trainings like

I'm not sure how easier I can explain it than the documentation or that training module. Salesforce also organises web-based trainings (don't think there will be any classrooms but you might want to give it a go: https://trailhead.salesforce.com/credentials/platformappbuilder

You should paste which concepts you struggle with, I hope it's not the whole page?

There are 7 events as you create/edit/delete data in salesforce.

Before

Sometimes you need to run some checks that are too complex to achieve with configuration (required fields, validation rules, unique fields). A "before" trigger is a great place for that - you could prevent saving if some complex condition isn't met. And then it's up to you if you want that check to happen just on creation or also on every edit of the record. Another example: you might want to prevent deleting record X if something. There are ways to do it with config but if it gets complex - a "before delete" trigger is great. A validation rule will not run on delete which limits your config options.

"Befores" are also great if you need to prepopulate some values. Maybe you need something like "if Contact's phone wasn't specified - pull it from related Account". You can do it with config but it's work, a "before" trigger might be neater and faster.

After Afters are good for "side effects". If I cloned an Opportunity - maybe would be nice to clone the Opportunity Line Items too. If I know the save succeeded - maybe I need to send some notification to finance system. Set some reminder tasks for 2 months from now to follow up with the client. Again - a lot of these can be done with config.

So what's with the "context variables"? Well, if you use all 7 events the whole trigger's body will execute 7 times. Which is nasty, you don't want to clone Opp Line Items on every edit, just once. You don't want to send that finance notification if you're deleting the record (well, maybe you do. but probably that will be different message). So the context variables can be used in if/switch statements to decide which code to run on which event.

It looks bit messy but it's better to have 1 trigger on object and call right functions than to have multiple triggers, separate for inserts, updates... It gets very nasty very soon. Salesforce will set these variables (you could call them global variables) before executing your code.

eyescream
  • 18,088
  • 2
  • 34
  • 46