• farhana
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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 }

 

 

 

Hello Everyone. I am very new to SalesForce development and would like to ask some questions regarding Triggers.

 

My company has a need to capture duplicate leads and process them by merging information.

 

After reading through documentation, I thought trigger would be the best way to do this.

 

To test the process, i created a custom object and wrote "before insert" trigger against it.

This trigger essentially searches for record based on email address and if record exists, it updates the visit count. However, I failed on this matter since after it processes the new record, it STILL inserts the duplicate record.

 

I've tried to use both before insert, after insert events.

My thought was, before insert, merge the record and after insert, delete the newly created record.

This also failed since I can NOT perform DML against trigger.new records.

 

Has anyone faced this type of issue before and if so, how did you resolve this?

 

Any assistance would be greatly appriciated.

 

thank you.