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
Ted OfficerTed Officer 

Duplicate notification

We have installed an Appliciaton which helps to identify the entered to the system is duplicate or no(based on the email address).

So here's what we need to accomplish:

Let's say the lead "John Smith, with email jsmith@gmail.com" is already in the system, then we create another lead "Jamie Smith, with email jsmith@gmail.com", and it automatically sets Lead Status to "New - Dupe Lead!", so now we have:

Lead Name - Email - Lead Status

John Smith - jsmith@gmail.com - "Lead"
Jamie Smith - jsmith@gmail.com - "New - Dupe Lead!"

 

Until this point it is working,s o we need a apex trigger(was told that by SF basic support) in salesforce to:

Find Lead_1 where Lead Status "New - Dupe Lead!"
Then
Find Lead_2 where Lead_1.email=lead_2.email // (so we are trying to find lead with the same email, but which is not dupe)
Then
Assign Lead_2.OwnerID=lead_1.onwerid
Send email to lead_2.ownerid //notify that this is a duplicate lead


What is the best way to accomplish this?

 

* SF Support said you can only do this by create an apex script, however we have no experience where to start. We have developers who could easily do the script, but we are just not sure where to start. 

 

Maybe an simple exmaple of any other script or what actually we need to create? Is it apex trigger or class, etc....

 

Thanks

CLKCLK

Lead 1 - John Smith - jsmith@gmail.com - "Lead"
Lead 2 -  Jamie Smith - jsmith@gmail.com - "New - Dupe Lead!"

 

to set Status to duplicate & owner of original one, have before trigger

 

Code Sniffet : // this code written by taking care of bulk operations, Governer limits

 

trigger setDuplicate on lead (before insert){

    map<id,string> mapEmail = new map<id,string>();

    for(Lead olead : trigger.new)
            mapEmail.put(oLead.Email,olead.id);

    List<Lead> lstLead = [select status,ownerid from Lead where status in : mapEmail.keyset() and id not in: trigger.newmap.keySet()];

    if(lstLead.isEmpty())
       return;
    
    for(Lead olead :lstLead){
            if(mapEmail.containsKey(oLead.Email)){
                Lead duplicatelead = trigger.newMap.get(mapEmail.get(oLead.Email));
                duplicatelead.Status = 'New - Dupe Lead!';
                duplicatelead.OwnerId = olead.OwnerID;
             }
       }
}

CLKCLK

And for sending mail to original owner for notification of duplicate lead

 

Set Workflow like this

 

Workflow Rule Criteria - Status = "New - Dupe Lead!"

 

Workflow Rule Action - Send mail to Owner

Ted OfficerTed Officer

Thanks for the help, one more question, where do I add this? is is Apex trigger or a class or..? I'm missing the very first step. Thanks again!

CLKCLK

First code is trigger

second step is WorkFlow

Ted OfficerTed Officer

Thanks again,

 

Please correct me if i did put into the wrong place. 

 

Setup > Customize > Leads > Trigger > New Trigger

 

Added the code, but when I try to save the lead - i get:

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger setDuplicate caused an unexpected exception, contact your administrator: setDuplicate: execution of BeforeInsert caused by: System.StringException: Invalid id: xx@dsd.com: Trigger.setDuplicate: line 6, column 13

 

Any ideas?

 

Thanks

CLKCLK
map<id,string> mapEmail = new map<id,string>(); change this line to.
map<string,id> mapEmail = new map<string,id>();
Ted OfficerTed Officer

Thanks for the promt reply, but now I'm getting:

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger setDuplicate caused an unexpected exception, contact your administrator: setDuplicate: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.setDuplicate: line 8, column 110

 

Any ideas?


Thanks!

CLKCLK

use this code, there were some mistakes

 

trigger setDuplicate on lead (before insert){

    map<string,id> mapEmail = new map<string,id>();

    for(Lead olead : trigger.new)
            mapEmail.put(oLead.Email,olead.id);

    List<Lead> lstExistingLead = [select status,ownerid,email from Lead where email in : mapEmail.keyset() and id not in: trigger.newmap.keySet()];

    if(lstExistingLead.isEmpty())
       return;
    
    for(Lead olead :lstExistingLead){
            if(mapEmail.containsKey(oLead.Email)){
                Lead duplicatelead = trigger.newMap.get(mapEmail.get(oLead.Email));
                duplicatelead.Status = 'New - Dupe Lead!';
                duplicatelead.OwnerId = olead.OwnerID;
             }
       }
}

CLKCLK

ohhh..sorry this should be working fine

 

trigger setDuplicate on lead (before insert){

    map<string,Lead> mapEmail = new map<string,Lead>();

    for(Lead olead : trigger.new)
            mapEmail.put(oLead.Email,olead);

    List<Lead> lstExistingLead = [select status,ownerid,email from Lead where email in : mapEmail.keyset()];

    if(lstExistingLead.isEmpty())
       return;
    
    for(Lead olead :lstExistingLead){
            if(mapEmail.containsKey(oLead.Email)){
                Lead duplicatelead = mapEmail.get(oLead.Email);
                duplicatelead.Status = 'New - Dupe Lead!';
                duplicatelead.OwnerId = olead.OwnerID;
             }
       }
}

Ted OfficerTed Officer

Thanks it worked!

 

However, I was trying to move it form sandbox to production(Deploy - Inbound/Outbound Change sets) and received the following messages:

 

setDuplicate   Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
Deploy Error   Average test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required.

 

How do I create those test? Thanks again.

CLKCLK

You need to write test class for this trigger

Ted OfficerTed Officer

Could someone help me with the test case for this trigger?

 

Thanks again.