You need to sign in to do that
Don't have an account?

Duplicate Insert trigger works but...
Hello,
I have an Apex Trigger that I needs to perform the following:
Thanks for the assistance.
M
I have an Apex Trigger that I needs to perform the following:
- prevent duplicate lead from being inserted during an import
- add task to existing Lead that was found within SF
trigger LeadPreventDuplicate on Lead (after insert,after update) { Map<String, Lead> leadMap = new Map<String, Lead>(); for (Lead lead : System.Trigger.new) { if ((lead.Email != null) && (System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email))) { leadMap.put(lead.Email, lead); } } List<task> addtask=New List<task>(); for (Lead lead : [SELECT Email FROM Lead WHERE Email IN :leadMap.KeySet()]) { Lead newLead = leadMap.get(lead.Email); addtask.add(new Task( WhoID =lead.id, Status = 'Active', Subject = 'Test Task', ActivityDate = system.today())); } insert addtask; Lead[] dupes = new Lead[0]; Set<String> email = new Set<String>(), dupEmail = new Set<String>(); for(Lead record: Trigger.new) email.add(record.email); email.remove(null); for(Lead record: [SELECT Id, Email FROM Lead WHERE Email IN :email]) if(!Trigger.newMap.containsKey(record.id)) dupEmail.add(record.Email); for(Lead record: Trigger.new) if(dupEmail.contains(record.Email)) dupes.add(record.clone(true)); else dupEmail.add(record.Email); delete dupes; }
Thanks for the assistance.
M
Here is the test class
All Answers
what is the purpose of this code. To me it looks like it's not needed
You can do something like this
Let me know if this help
Thanks for the reply. The task should be created and associated to existing lead within Salesforce. NOT to the newly inserted Lead. The second half of the trigger is where I was trying to delete the duplicates that were being imported. Instead I might just flag them as a duplicate(checkbox) and allow the user to delete them at a later date. That seems like a safer approach.
So for this portion of the triggerI was trying to just add a task to the existing Leads.
The reason I do not want to add the same task to the new leads is that it may be confusing to the end user if existing leads and the newly created leads have the same Activty. How can I break up the above code to create a specific lead for existing Leads and a different Lead for the newly inserted Leads.
Thank you for the feedback.
Regarding the task, I understand you wanted to add the task for the existing lead and not for the new lead but my question is
We utilize Task to record customer engagements. So if the Lead exist we need to record that tghe customer performed another engagement and not create another Lead with the same email address.
Thanks,
M
I mistyped the above information. Here is what I was trying to say.
The reason I do not want to add the same task to the new leads is that it may be confusing to the end user if existing leads and the newly created leads have the same Activty message. How can I break up the above code to create a specific activty for existing Leads and a different activty for the newly inserted Leads.
Thanks
it will only create a task against the existing lead.
Let me know if it will work for you or not.
Thank you for the reply and help.
I am going to have to thoroughlt read threw the code to make sure I understand what each line of code is doing.
The line of code Will allow the duplicate lead to be created and the checkbox set to true?
The 2 for loops at the bottom of the trigger is for adding an activty to an existing Lead within Salesforce correct? The only difference is that the commented out one has an additional criteria. Correct?
Thanks for all your help.
M
Apologies on the delayed response. I was pulled to another project. I tested the above code and this is what I am seeing.
Here is the Apex Class. It was determined to be safer to not delete the duplicate but to flag them as a duplicate. Thanks for the help and your time,
M
This is so close. Only remaining issue is that for some reason the activity is being added to the first inserted lead eventhough it is not a duplicate.
The new lead is not in the leadMap.KeySet so I am not sure why the new non duplicate lead is having an activty added to it.
Thanks for staying with this Abdul.
M
should I also be filtering out Leads where the checkbox is set to true? These records will never have abn activty added to them.
Here is an example:
A lead with an email address of test@gmail.com is imported into Salesofrce on 01012019
This email address does not exist so the lead is created without an activty.
On 01032019 a lead is imported with an email address of test@gmail.com. This lead is created and flagged as duplicate.
The lead with the email address test@gmail.com that was created on 010102019 will have an activty attached.
In a perfect scenerio the first lead would have an activty stating that the first customer activty was created on when teh lead was first imported. Then all following activties would have a different message stating that a duplciate with the same email address was submitted into salesforce.
But right now if we can just the first scenerio working that would be an great.
M
Here is the test class
You nailed it, thank you very much. If you have time, would you put some comments on the trigger? This way I and others can fully understand the logic you used to resolve this problem.
For example in line Could I insert a specific task for this Lead sinc ethis is the first non duplicate lead?
Should all of the queries for the Lead object include where DupcheckBox = false)
Thanks again for all your help.
Cheers,
M
I personally don't like putting comments in the code, to me it muggy the code. Normally I design my code in parts by refactoring the code that takes time therefore I can't do that here.
As people look for quick solution here and developers here wanted to be the first to provide them so I do the same.
but I guess I divided my code in different session so it should be that hard to understand
I do not understand this part of yours, please clarify.
Could I insert a specific task for this Lead sinc ethis is the first non duplicate lead?
The business model that we use is that we record Consumer actions as Activty records on a Lead record. So the Lead record is basically a shell that housesd all of the csutomer engagements(Activity records).
So if we go back to my prior example
A lead with an email address of test@gmail.com is imported into Salesofrce on 01012019
This email address does not exist so the lead is created and an activcty is created to specificy this was the customers first engagement
On 01032019 a lead is imported with an email address of test@gmail.com. This lead is created and flagged as duplicate.
The lead with the email address test@gmail.com that was created on 010102019 will have an activty attached stating this was followup engagement.
And theoritacally I could now assign a task to a user when the lead count record meets a certain number?
The trigger picks up that there 4 duplicate leads this could be our target threshold to send the inforamtion to our inside sales team since the customer has engages with a certain number of time.
This code is definitly opening up some possibilities for me to automate of business processes. Thank you for all your help.
M
Hope you had a good wekend. Here are my responses to your questions
I asked this question specific to Task Assignment because in the above code I don't see you are any line that says as an example. So curios to know where the assignment is happening for all these task are getting created.