0

I'm somewhat new to Salesforce development, and I have a task from the architect to create a Before trigger to map a field upon Lead Conversion. This Lead field is already mapped through the native mapping , to an opp field, and needs to mapped to a second one by this trigger, only after the conversion.

I have three doubts: 1 - it shouldn't be a after trigger, since the mapping only happens after lead conversion?

2 - A process builder wouldn't be better in this scenario instead of a trigger?

3 - I'm stuck in how to begin to create the trigger, if some can send me a example it will much appreciated.

Thanks for the help!

  • Can you provide what object the second field is on? also Oppty? – stackasaur Jun 24 at 13:03
  • Yes, is also in Oppty! To make more clear, is one field in Lead, that needs to be mapped to two distintc fields in Oppty. One is already made, through th native mapping. – Growred Jun 24 at 13:07
2

These are all good questions. You'll see that the answers are generally specific to your setup.

  1. I'd say this answer on Lead Conversion and order of executions will provide you value. In short, you could do before if you have Enable Validation and Triggers from Lead Convert setting checked in the Setup > Customize > Leads > Settings page. After might still make more sense if you're just looking to update related records.

  2. You can do things in Salesforce in many different ways with the automation tools at your disposable (ex. Workflow, Process Builder, Flow, Apex). The best practice from Salesforce is to pick one tool per object and stick with it. This might be a case where you already have a trigger and it wouldn't make sense to add a process builder. However, it might be the case where you have no code and it would make sense. Again, this is specific to your situation and something you should bring up as that should be a good topic to discuss

  3. I would take a look at the trigger trailhead. Once you understand the basics on triggers, it's worthwhile to learn about frameworks as there's many and again this is something specific to your situation. Your org may already have a framework in practice and likewise your team members may want to discuss this. I'll throw in the Apex Best Practices as another good thing to reference.

| improve this answer | |
  • Thanks for the awesome Response Kris. To lit up the discussion: 1 - Yes it is enabled! 2 - Well, In Oppty, this is the first story we're working in, so it doesn't have yet, any trigger assigned to it. And the Story's requisite tells to write the trigger in Oppty. It would be a case to raise a hand and discuss the probabilty of PB? 3 - I'll look in all of them! – Growred Jun 24 at 13:21
  • Yes. I don't think there's any harm in asking whether a process builder would make more sense on a given object that doesn't have any code yet. – Kris Goncalves Jun 24 at 14:04
0

Answering your first question, updating field on before trigger you won't need to use update DML operation like you would have to make that in after trigger, so that's may be the trick (why on before and not on after). Considering your third question, here is a simple example of how to declare triggers:

trigger HelloWorldTrigger on Account (before insert) {
    System.debug('Hello World!');
}

You can find more on the trailhead or in docs. Actually I would like to suggest you read some info before using triggers. There are a lot of tricky parts of them. One of them is handling bulk records.

| improve this answer | |
  • Thanks Azimut, Thanks for the part of the DML statement, I think I would be stuck in it at some part. – Growred Jun 24 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.