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
Vidhyasagar RanganathanVidhyasagar Ranganathan 

Help to create trigger to update records

Hello,

I am trying to create a trigger that will update the custom object records once records are created in the object. The custom object has ID from Lead Object and with that I will have to pull 5 fields of that lead and update in the custom object.

I started with the trigger but couldn't proceed and any help is appreciated

Thanks

trigger CampaignAttributionGetLead2 on Campaign_Attribution__c (before insert, before update) {
    for (Campaign_Attribution__C C: Trigger.new){
        if(C.Lead_ID__C != null)
        {
            List<Lead> L = [SELECT createddate, name, company, status, Lead_Medium__c FROM lead where ID = :C.Lead_ID__c] ;            
            {
                C.Lead_Created_Date__c = L.createddate;
                C.Lead_Medium__c = L.Lead_Medium__c;
                C.Lead_Status__C = L.Status;
                C.Lead_Name__C = L.name;
                C.Lead_Company__C = L.company;
            }
        }            
    }
    update C;
}
v varaprasadv varaprasad
Hi Vidyasagar,

Please check once below sample code:
 
trigger CampaignAttributionGetLead2 on Campaign_Attribution__c (After insert, After update) {
   
   set<id> leadIds = new set<id>();
   
   for (Campaign_Attribution__C C: Trigger.new){
        if(C.Lead_ID__C != null)
        {
         leadIds.add(C.Lead_ID__C);  
        }            
    }
   List<Lead> ListOfleads = [SELECT id,createddate, name, company, status, Lead_Medium__c FROM lead where ID in :leadIds];

   for(lead ld : ListOfleads){
   for(Campaign_Attribution__C C: Trigger.new){
   if(ld.id == C.Lead_ID__C){
       C.Lead_Created_Date__c = ld.createddate;
                C.Lead_Medium__c = ld.Lead_Medium__c;
                C.Lead_Status__C = ld.Status;
                C.Lead_Name__C = ld.name;
                C.Lead_Company__C = ld.company;
   }
              
   
   }
  
  
  
  }   
   
   
}

I have not tested above code may you will get some syntactical errors.


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com



 
Rahul.MishraRahul.Mishra
Hi Vidyasagar,

try the following code:
 
trigger pullData on Campaign_Attribution__C (after insert, after update) {
    
    Map<Id, list<Campaign_Attribution__C >> mapLeadtoCampaign = new Map<Id, List<Campaign_Attribution__C >>();
    List<Campaign_Attribution__C > lstCamPaignToUpdate = new List<Campaign_Attribution__C >();
    
    for(Campaign_Attribution__C CA: trigger.new) {
        if(CA.Lead_ID__C != Null && trigger.isInsert) {      
            
            if(!mapLeadtoCampaign.containsKey(CA.Lead_ID__C)) {
                List<Campaign_Attribution__C > lstCam= new List<Campaign_Attribution__C >();
                lstCam.add(CA);
                mapLeadtoCampaign.put(CA.Lead_ID__C , lstCam);
            } else {
                mapLeadtoCampaign.get(CA.Lead_ID__C ).add(CA);
            }
            
        }
        
        
        if(CA.Lead_ID__C != Null && trigger.isUpdate && trigger.oldMap.get(CA.Id).Lead_ID__C != CA.Lead_ID__C) {      
            
            if(!mapLeadtoCampaign.containsKey(CA.Lead_ID__C)) {
                List<Campaign_Attribution__C > lstCam= new List<Campaign_Attribution__C >();
                lstCam.add(CA);
                mapLeadtoCampaign.put(CA.Lead_ID__C , lstCam);
            } else {
                mapLeadtoCampaign.get(CA.Lead_ID__C ).add(CA);
            }
            
        }
        
        
    }
    
    For(Lead ld: [Select createddate, name, company, status, Lead_Medium__c FROM Lead Where Id =:mapLeadtoCampaign.keySet()]) {    
        for (Campaign_Attribution__C c: mapLeadtoCampaign.get(ld.Id)) {
            
            lstCamPaignToUpdate.add(new Contact(Id = c.Id, Lead_Created_Date__c = ld.createddate, Lead_Medium__c = ld.Lead_Medium__c
                                                Lead_Status__C = ld.Status, Lead_Name__C = ld.name, Lead_Company__C = ld.company));
            
        }
        
    }
    if(!lstCamPaignToUpdate.isEmpty())
        update lstCamPaignToUpdate;
}

Mark best if does helps you.

Thanks,
Rahul​
Akshay_DhimanAkshay_Dhiman
Hi Vidyasagar, 
TRY THIS CODE:
trigger CampaignAttributionGetLead2 on Campaign_Attribution__c (before insert,before update) {
    Set<Id> sid =  new Set<Id>();
    List<Lead> llList  = new List<Lead>();
    List<Campaign_Attribution__c>  newList = new List<Campaign_Attribution__c>();
   
    for(Campaign_Attribution__c ac:Trigger.New){
        sid.add(ac.Lead_ID__c);
    }
    System.debug('sid:'+sid);
    llList = [SELECT CreatedDate, Name, Company, Status FROM Lead where Id IN: sid];
  
    System.debug('llList:'+llList);
   // System.debug('newList:'+newList);
    for(Lead lll :llList){
        for(Campaign_Attribution__c ca:Trigger.New){
            if(ca.Lead_ID__c==lll.Id){
                ca.Name= lll.Name;
                ca.CreatedDate__c = lll.CreatedDate;
                ca.Lead_Medium__c = lll.Lead_Medium__c;
                ca.Status__C = lll.Status;
                ca.Company__C = lll.company;
              
            }
           
        }
        
    }
    
    
}


Thanks
Akhsay
Vidhyasagar RanganathanVidhyasagar Ranganathan
Thank you all for your help and appreciate it.

I just did some changes and it worked well.