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
Adam Lee 60Adam Lee 60 

Bringing back Lead Name using Trigger on Case

Hi all.

I am very new to code so can someone please give me some advice?

I am trying to create trigger for when a case is created.

This is what I want to do:

if email address is on a lead, then populate the 'Lead' field. otherwise just leave the Lead field blank and save.
trigger UpdateLeadOnCase on Case (before update)
 {
    public id leadID;
    public id caseId;
    public string caseEmail;
    
    Id caseRT1 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Inbound MTB Emails').getRecordTypeId(); 
    Id caseRT2 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Inbound Sponsors').getRecordTypeId(); 
    Id caseRT3 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Inbound Summits Del EU').getRecordTypeId(); 
    Id caseRT4 = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Inbound Summits Del US').getRecordTypeId(); 
    
    for(Case ca: Trigger.New)
    { 
        try
        {
          if(ca.SuppliedEmail!=null)
          {
            Lead L = [Select ID,Name from Lead where Email =: ca.SuppliedEmail limit 1];
            ca.Lead__c=l.Id;
          }
         else
         {
          //ca.Lead__c='';
         }
        }
        catch(Exception ex)
        {
          ca.addError('No email address is found');

        }
       
    }
    

}

I've put an exception in at the moment so I can save the trigger but this needs replacing,

Hope you can help.

Thanks in advance
Roshni RahulRoshni Rahul
Hi Adam,

In line number 19, what is  ca.Lead__c ?
rajat Maheshwari 6rajat Maheshwari 6

Hi Adam,

Please follow this below code snippet and let me know in case of any help

trigger caseTrigger on case(before insert,before update)
  {
    Map<String,Lead> mp_StrLead;
    List<Case> cs_List = new List<Case>();
    Set<String> st_String = new Set<String>();
     if( Trigger.isInsert)
          {
               for(Case cs : Trigger.new)
                   {
                       st_String.add(cs.SuppliedEmail);
                    }
           }

         if(Trigger.isUpdate)
              {
                   for(Case cs : Trigger.new)
                       {
                             if(cs.SuppliedEmail!=null && cs.SuppliedEmail != Trigger.oldMap.get(cs.id).SuppliedEmail)
                                      {
                                              st_String.add(cs.SuppliedEmail);
                                      }
                        }
              }
                                   
if(st_String !=null && !st_String.isEmpty())
   {
         for(Lead ld : [Select Id,FirstName,LastName,Email from lead where Email IN:st_String])
             {
                   if(mp_StrLead==null)
                         mp_StrLead = new Map<String,Lead>();

                       mp_StrLead.put(ld.Email,ld);
             }

      for(Case cs : Trigger.new)
           {
               if(cs.SuppliedEmail!=null && mp_StrLead!=null && mp_StrLead.containsKey(cs.SuppliedEmail)
                   {
                         cs.Lead__c = mp_StrLead.get(cs.SuppliedEmail).id;
                         cs_List.add(cs);
                    }
             }

if(cs_List!=null && !cs_List.isEmpty())
       update cs_List;

}

}



 
rajat Maheshwari 6rajat Maheshwari 6

Hi Adam,

Please use this below code snippet, ignore last one : - 

trigger caseTrigger on case(before insert,before update)
  {
    Map<String,Lead> mp_StrLead;
    
    Set<String> st_String = new Set<String>();
     if( Trigger.isInsert)
          {
               for(Case cs : Trigger.new)
                   {
                       st_String.add(cs.SuppliedEmail__c);
                    }
           }

         if(Trigger.isUpdate)
              {
                   for(Case cs : Trigger.new)
                       {
                             if(cs.SuppliedEmail__c!=null && cs.SuppliedEmail__c != Trigger.oldMap.get(cs.id).SuppliedEmail__c)
                                      {
                                              st_String.add(cs.SuppliedEmail__c);
                                      }
                        }
              }
                                   
if(st_String !=null && !st_String.isEmpty())
   {
         for(Lead ld : [Select Id,FirstName,LastName,Email from lead where Email IN:st_String])
             {
                   if(mp_StrLead==null)
                         mp_StrLead = new Map<String,Lead>();

                       mp_StrLead.put(ld.Email,ld);
             }

      for(Case cs : Trigger.new)
           {
               if(cs.SuppliedEmail__c!=null && mp_StrLead!=null && mp_StrLead.containsKey(cs.SuppliedEmail__c))
                   {
                         cs.Lead__c = mp_StrLead.get(cs.SuppliedEmail__c).id;
                         
                    }
                    
               else
                 {
                     cs.Lead__c = null;
                 }
             }



}

}
rajat Maheshwari 6rajat Maheshwari 6

Hi Adam,

Please mark the best answer, if it works for you :), otherwise let me know the issue, if you are still facing.

Thanks