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
agieragier 

Errors in trigger for preventing duplicate leads in Cookbook

I'm trying to test the sample provided on p. 118-119 of the Cookbook to prevent duplicate leads based on email address. I have come across two errors so far in the trigger:
 

Error: Compile Error: expecting a right parentheses, found 'get' at line 10 column 1

 

I seem to be able to get past this error by removing "get"

 

Error: Compile Error: Method does not exist or incorrect signature: [MAP:Id,SOBJECT:Lead].(Id) at line 9 column 16

I am not sure I know what is causing this error or how to fix. Any thoughts??

 

Here is the trigger from the cookbook:

 

trigger leadDuplicatePreventer on Lead (before insert, before update) {

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.');

}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.');

}

}

 

 

 

CaptainObviousCaptainObvious

System.Trigger.oldMap.get(lead.Id).Email should go on the same line, but it appears to be a new line as it's printed in the cookbook :smileyvery-happy:

The following code is working for me:

Code:
trigger leadDuplicatePreventer on Lead (before insert, before update) {

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.');

} 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.');
}
}


 

 

agieragier
Ah ha! Thank you so much! It is now working :womanvery-happy:
CTU007CTU007

I am playing with this, it works in my developer account, then I tried to deploy it into my production. I followed the instructions on the apex programmer guide(installed SDK, ANT, MIGRATION TOOL), but I cannot deploy it.

error msg:

Code coverage issue, class: leadDuplicatePreventer -- test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

Code coverage issue -- Average test coverage across all apex classes and triggers is 0%, at least 75% test coverage is required

I then tried to save the test class on page 81 of the cookbook, but it cannot save with the following error:
 
Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 47 column 45
 
Line 47 is:             System.assert(e.getDmlFields(0)[0] == 'Email');
 
So how to deploy it? Any chance we can modify this trigger to prevent duplicate contacts?
 
Thanks, I have no knowledge on Java, Ant, etc.
 
msreekmmsreekm

replace
System.assert(e.getDmlFields(0)[0] == 'Email');
with
System.assert(e.getDmlFields(0)[0] == Lead.Email);


you will need to replace all lines where error is occuring..

 

Cheers!

Sree