• Nick Milsner
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hi everyone, 

I'm writing a handler class called via a before insert trigger to prevent leads from getting created if they have a matching email across standard objects (account, contact, and lead). When I test my code in the anonymous window, I'm using an email that is planted across the three objects. Although my end goal is to prevent the creation of a lead if a matching email is found, I'm having trouble even seeing results in the debug log of parameters passed into my System methods. It would be super helpful if a member of this community can review my code and let me know where you think there are any holes and why it may be that my trigger and class aren't working properly. Thank you in advance! 
trigger MasterLeadTrigger on Lead (before insert, before update, before
    delete, after insert, after update, after delete,  after undelete) {
        
        List<Lead> newList = Trigger.new;
        
        MasterLeadTriggerHandler handler = new MasterLeadTriggerHandler();
        
		if(Trigger.isInsert && Trigger.isBefore){
            handler.OnBeforeInsert(Trigger.new);
        }
}

public class MasterLeadTriggerHandler {
    public void OnBeforeInsert(List<Lead> listNew){
        if(listNew.isEmpty()){
            System.debug('List is empty');
        }else{
            System.debug(listNew.size());
        }
        Set<String> email = new Set<String>();
        for(Lead newLead : listNew){
            if(newLead.Email != null){
                email.add(newLead.Email);
                System.assertEquals(email.size(),1);
            }
        }
        List<Lead> dupeLead = [SELECT Id FROM Lead WHERE Email IN :email 
                              OR Secondary_Email__c IN :email
                              OR Additional_Email__c IN :email];
        for(Lead newLead : listNew){
            if(dupeLead.size()>0){
                newLead.addError('This is a duplicate: '+dupeLead[0].Id);
            } update newLead;
        }
	}
}

 
Has anyone ran across this error when validating their components? SFDCAccessControllerTest is managed by Pardot... System.AssertException: Assertion Failed: Expected: false, Actual: true
Stack Trace: Class.SFDCAccessControllerTest.testAccessControl: line 53, column 1 
Hi everyone, 

I'm writing a handler class called via a before insert trigger to prevent leads from getting created if they have a matching email across standard objects (account, contact, and lead). When I test my code in the anonymous window, I'm using an email that is planted across the three objects. Although my end goal is to prevent the creation of a lead if a matching email is found, I'm having trouble even seeing results in the debug log of parameters passed into my System methods. It would be super helpful if a member of this community can review my code and let me know where you think there are any holes and why it may be that my trigger and class aren't working properly. Thank you in advance! 
trigger MasterLeadTrigger on Lead (before insert, before update, before
    delete, after insert, after update, after delete,  after undelete) {
        
        List<Lead> newList = Trigger.new;
        
        MasterLeadTriggerHandler handler = new MasterLeadTriggerHandler();
        
		if(Trigger.isInsert && Trigger.isBefore){
            handler.OnBeforeInsert(Trigger.new);
        }
}

public class MasterLeadTriggerHandler {
    public void OnBeforeInsert(List<Lead> listNew){
        if(listNew.isEmpty()){
            System.debug('List is empty');
        }else{
            System.debug(listNew.size());
        }
        Set<String> email = new Set<String>();
        for(Lead newLead : listNew){
            if(newLead.Email != null){
                email.add(newLead.Email);
                System.assertEquals(email.size(),1);
            }
        }
        List<Lead> dupeLead = [SELECT Id FROM Lead WHERE Email IN :email 
                              OR Secondary_Email__c IN :email
                              OR Additional_Email__c IN :email];
        for(Lead newLead : listNew){
            if(dupeLead.size()>0){
                newLead.addError('This is a duplicate: '+dupeLead[0].Id);
            } update newLead;
        }
	}
}