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
mhanafimhanafi 

How to allow all users to check for Duplicate Email using a trigger without data sharing.

I created a trigger to check for duplicate and raise an error.

It woks fine for Admin users.

Howevere when run by sales users they have the following error : 

 

Review all error messages below to correct your data.
Apex trigger setOwnerFromPrevious caused an unexpected exception, contact your administrator: setOwnerFromPrevious: execution of BeforeInsert caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com about custom indexing. Even if a field is indexed a filter might still not be selective when: 1. The filter value includes null (for instance binding with a list that contains null) 2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times):

 

 

 

Trigger DuplicateLead on Lead (before insert) {
Map<String, Lead> leadMap= new Map<String, Lead>();

for (Lead lead : System.Trigger.new) {
if(lead.Email !='' && lead.Email != null )
leadMap.put(lead.Email, lead);
}
if(leadMap.size()>0){
for (Lead lead : [SELECT Email,OwnerId,Account_Application__c,LeadSource,Account_Application_Date__c,ConvertedDate FROM Lead WHERE Email IN :leadMap.KeySet() Limit 1000])
{
Lead newLead = leadMap.get(lead.Email);
if(newLead!=null){
newLead.Email.addError('Email already exist');
}
}
}
}
 

 Any help will be greatly appreciated.

Message Edited by mhanafi on 06-17-2009 05:58 AM
MakMak

You are tripping the limit as there are too many rows to look at. You need to set an index. 

Check this post: http://forums.sforce.com/sforce/board/message?board.id=apex&message.id=2882 

mhanafimhanafi

The trigger run fine when run from the API(usually 100 leads inserted at the time).

Howevetr it fails for some users (non admin) who enter manually one  lead at the time.