Trying to write a dedupe trigger for the lead. Basically, if the lead is created then it will see if it has an exact match to another lead. If it does then it will update the new lead to show the primary lead as a lookup. My soql seems to not be saving when I am trying to have an in clause twice?

trigger FindDupes on Lead (before insert) {
for (Lead myLead : Trigger.new) {
if (myLead.Email != null) {
  List<Lead> dupes = [SELECT Company,Email,Id,Is_Primary__c,Primary_Lead__c,RecordTypeId FROM Lead
                           WHERE Email = :myLead.Email AND Company = :myLead.Company];
  if (dupes.size() > 0) {
    myLead.Primary_Lead__c = dupes[0].Id;
  } else {
    myLead.Primary_Lead__c = null;
    myLead.Is_Primary__c = True;
  • what error message do you see?
    – krigi
    Commented Sep 27, 2019 at 21:53
  • 3
    As an aside, consider using a Duplicate Rule instead. It will let you see dupes and/or choose to block them based on your configuration. It also adds a section where you can see duplicate records.
    – sfdcfox
    Commented Sep 27, 2019 at 21:58
  • When I change that I get the following errors. Unexpected token 'myLead.Company'. Missing 'IDENTIFIER' at ']' expecting a colon, found 'myLead.Company'
    – SoleHack
    Commented Sep 27, 2019 at 22:07
  • We are trying to update to show what the primary record was and not black any of them.
    – SoleHack
    Commented Sep 27, 2019 at 22:08
  • 1
    I just want to add that you should run that query outside of the for loop (imagine a dataload with 200 Leads for example. that would break the SOQL limit). Collect the emails and companies first and use those to query the dupes, and then put them in a map and use that to find the dupes when going through the Leads.
    – Roti
    Commented Sep 27, 2019 at 23:35

1 Answer 1


Your problem is the &&. You need to use the word AND instead. Similarly, you would use OR for an either-or filter.

  • sfdcfox, He is using AND in SOQL
    – Sarvesh
    Commented Sep 30, 2019 at 6:51
  • @Sarvesh Originally, OP was not, it has been edited.
    – sfdcfox
    Commented Sep 30, 2019 at 9:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .