You need to sign in to do that
Don't have an account?
Rory McDonnell
Issues with before/after insert logic
Hi, I am still in the early days of my apex journey so trying to get my head around basic concepts.
I have built a simple enough trigger which does the following. When somebody is creating or update a lead it looks at the values in 6 key fields and then counts the number that are not null. It then enters that value in the 'Key fields' field on the lead record.
Secondly if this value is greater than or equal to three it creates a task for each of the completed key fields.
So far I have gotten the first stage of the process to work by using before insert and before update. However now since the task record will require a lead ID I suspect I will need to use 'after update/insert' capabilities.
What would you suggest is the best way to modify my code to account for these two requirments.
I have built a simple enough trigger which does the following. When somebody is creating or update a lead it looks at the values in 6 key fields and then counts the number that are not null. It then enters that value in the 'Key fields' field on the lead record.
Secondly if this value is greater than or equal to three it creates a task for each of the completed key fields.
So far I have gotten the first stage of the process to work by using before insert and before update. However now since the task record will require a lead ID I suspect I will need to use 'after update/insert' capabilities.
What would you suggest is the best way to modify my code to account for these two requirments.
trigger KeyFieldsPopulated on Lead (before insert, before update) { for (Lead l : Trigger.New) { //Create list to store key fields List<String> KeyFields = new List<String>(); KeyFields.add(l.FirstName); KeyFields.add(l.LastName); KeyFields.add(l.Email); KeyFields.add(l.Phone); KeyFields.add(l.Website); KeyFields.add(l.Title); //Create list to store key fields that have a value List<String> KeyFieldsPopulated = new List<String>(); //Create a counter variable to track how many key fields have a value Integer counter = 0; //Loop through the key fields for (Integer i = 0; i<KeyFields.size(); i++) { if (KeyFields[i]!=null) { KeyFieldsPopulated.add(KeyFields[i]); counter = counter + 1; } l.Key_Fields__c = counter; } //Loop through the populated key fields and create a task if (counter >= 3) { for (Integer j = 0; j < KeyFieldsPopulated.size(); j++) { Task t = new Task(); t.Subject = 'Verify the '+ KeyFieldsPopulated[j] + ' field'; t.WhatId = l.Id; } } } }
You have completed the forst step very well according to the requirement and as far as in the second step If you want to give the lookup of the lead on the Task Object then Go ahead for the after trigger because in that case only, I think you will need the Id of the lead but if you want to create a Task only by calculating the Lead field then you should go for before trigger.
For more information about before and after Please go through the following link .
https://www.sfdc99.com/2014/01/25/use-vs-triggers/
https://salesforce.stackexchange.com/questions/96263/what-is-the-exact-difference-between-before-update-and-after-update-trigger
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com