+ Start a Discussion
SId.New devSId.New dev 

Trigger not working

Hi all.

concatinate_name_and_phone__c,concatinate_name_and_email__c are formula fields which holds (fullname+email)&(fullname+phone)
i need is, i need to show error when ever (fullname+email) or (fullname+phone) exists.Im not getting where iam missing.

Is formula fields works before insert????

This trigger shows an error message when email+name or name+phone already exists and give a old record link to open that record.
 This trigger shows an error message when email already exists and give a old record link to open that record. 

trigger TriggerOnLead on Lead (before insert) {
 
    Map<String, Lead> mapLead1 = new Map<String, Lead>();
    SYSTEM.DEBUG('!!!!!!!!!!mapLead1!!!!!!!!!!!'+mapLead1);
    Map<String, Lead> mapLead2 = new Map<String, Lead>();
    SYSTEM.DEBUG('+++++++++++mapLead2 +++++++++++++++'+mapLead2 );

    for (Lead lead : System.Trigger.new) {
          mapLead1.put(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM(), lead);
          mapLead2.put(lead.concatinate_name_and_phone__c.TOLOWERCASE().TRIM(), lead);
    }
     list<lead> lead1= new list<lead>([SELECT Id,Name,Email,Phone,concatinate_name_and_email__c FROM Lead WHERE concatinate_name_and_email__c  IN :mapLead1.KeySet()]);
   
    list<lead> lead2=[SELECT Id,Name,Email,Phone,concatinate_name_and_phone__c FROM Lead WHERE concatinate_name_and_phone__c IN :mapLead2.KeySet()];
    for (Lead lead4 : System.Trigger.new) {
      if(lead4.Any_way_save_record__c!=true)
      {
  
     if (lead.concatinate_name_and_email__c != null) 
    {
     for (Lead lead :lead1){
      Lead newLead = mapLead1.get(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM());
       SYSTEM.DEBUG('*************mapLead1************************'+mapLead1);
         SYSTEM.DEBUG('@@@@@@@@@newLead@@@@@@@@@@@@@@@'+newLead);
        newLead.addError('see lead<a style=color:GREEN href=/apex/leaderror?id='+LEAD.ID+'>'+  lead.name + '</a>');
  
    }}else  if (lead.concatinate_name_and_phone__c != null) 
    for (Lead lead :lead2 ) {
      Lead newLead1 = mapLead2.get(lead.concatinate_name_and_phone__c.TOLOWERCASE().TRIM());
      SYSTEM.DEBUG('%%%%%%%%%%%%newLead1%%%%%%%%%%%%%%%%%%'+newLead1);
      
        
       newLead1.addError('see lead<a style=color:GREEN href=/apex/leaderror?id='+LEAD.ID+'>'+  lead.name + '</a>');

  
    }
    
   }
}
}

 

 it is saving with out giving any error(when phone&Name  is given same

 

Best Answer chosen by Admin (Salesforce Developers) 
s_k_as_k_a

Hi,

 

The is the modified code to show the error when already lead exists.

 

trigger TriggerOnLead on Lead (before insert) {
 
   Set<String> nameAndEmail = new Set<String>();
   Set<String> nameAndPhone = new Set<String>();

    for (Lead lead : Trigger.new) {
          nameAndEmail.add(lead.concatinate_name_and_email__c.TOLOWERCASE().TRIM());
          nameAndPhone.add(lead.concatinate_name_and_phone__c.TOLOWERCASE().TRIM());
    }
    system.debug(nameAndEmail);
    system.debug(nameAndPhone);
    list<lead> leads= [SELECT Id,Name,Email,Phone,concatinate_name_and_email__c,concatinate_name_and_phone__c FROM Lead WHERE concatinate_name_and_email__c  IN :nameAndEmail or concatinate_name_and_phone__c in :nameAndPhone];
    system.debug(leads);
    if(!leads.isEmpty())
    {
        for(lead newlead : trigger.new)
        {
            for(lead l: leads)
            {
                if(l.concatinate_name_and_email__c != null && l.concatinate_name_and_email__c == newlead.concatinate_name_and_email__c)
                { 
                    newLead.addError('see  the lead already exists with that email and phone:--'+'\n'+l.Name);
                }
                else if(l.concatinate_name_and_phone__c != null && l.concatinate_name_and_phone__c == newlead.concatinate_name_and_phone__c)
                 {
                    newLead.addError('see  the lead already exists with that email and phone:--'+'\n'+l.Name);           
                 }
            }
        }
     }
 }