function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
farhanafarhana 

Upsert with related fields - duplicated record

hi everyone,

I am new to it, I have create a custom object for leads called car inquiry. Each lead come with a car inquiry, and one email address can have sent us multiple car inquiries. SO I create a custom car object called it car_inquiry.

Here is what I want to achieve:

step 1- If it is a new car inquiry, create a new lead, and send car_inquiry details to the custom carinquiry object. I am able to do that.

 

Step2- Prevent duplicate leads if a lead from same email address already exist. I am able to do this to by using cookbook code.

 

Step3 - If it is a duplicate lead, create another car inquiry under existing lead.I am able to do this to. BUT...

problem:

 

Problem is when it is a dulicate lead, it not only updates the existing lead by creating a new car inquiry but it also creates a new lead. If I send third leads with same email address, it updated existing two leads with same email address and then create a new lead with new car inquiry.Here is my code.

 

Please some help me. I am not beginner.

trigger BPX_Upsert on Lead (after insert, before insert, before update) { if (Trigger.isBefore) { Map<String, Lead> leadMap = new Map<String, Lead>(); for (Lead lead : System.Trigger.new) { /* Make sure we don't treat an email address that isn't changing during an update as a duplicate. */ if ((lead.Email != null) && (System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email))) { // Make sure another new lead isn't also a duplicate if (leadMap.containsKey(lead.Email)) { lead.Email.addError('Another new lead has the same email address.'); // after leads are inserted } else { leadMap.put(lead.Email, lead); } } } /* Using a single database query, find all the leads in the database that have the same email address as any of the leads being inserted or updated. */ for (Lead lead : [SELECT Email FROM Lead WHERE Email IN :leadMap.KeySet()]) { Lead newLead = leadMap.get(lead.Email); // newLead.Email.addError('A lead with this email address already exists.'); // If email address already exists, add another inquiry in custom car inquiry List<Inquiries__c> followupTasks = new List<Inquiries__c>();// build list in memory Inquiries__c inq = new Inquiries__c( Lead__c = lead.Id, Year__c = '2013'); followupTasks.add(inq); // add to list // insert as caar inquiry insert followupTasks; } } else if (Trigger.isAfter) { // If it is a new lead and it has been inserted, Also add the car inquiry List<Inquiries__c> followupTasks = new List<Inquiries__c>(); // build list in memory for (Lead lead : System.Trigger.new) { Inquiries__c inq = new Inquiries__c( Lead__c = lead.Id, Year__c = '2014'); followupTasks.add(inq); // add to list } // insert the entire list insert followupTasks; // NOTE: this is outside the above loop, only one insert is needed } // end of isAfter }

 

 

 

HarmpieHarmpie

Not quite sure, but shouldn't you uncomment :

// newLead.Email.addError('A lead with this email address already exists.');

 

If you don't, it will be inserted anyway (including the inquiry) and thus enter the isAfter block (with another inquiry).

 

Also, I noticed 

 

for (Lead lead : [SELECT Email FROM Lead WHERE Email IN :leadMap.KeySet()]) { Lead newLead = leadMap.get(lead.Email); // newLead.Email.addError('A lead with this email address already exists.'); // If email address already exists, add another inquiry in custom car inquiry List<Inquiries__c> followupTasks = new List<Inquiries__c>();// build list in memory Inquiries__c inq = new Inquiries__c( Lead__c = lead.Id, Year__c = '2013'); followupTasks.add(inq); // add to list // insert as caar inquiry } insert followupTasks;

 

will probably be the better approach