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
Kiru535Kiru535 

Populating Lead object custom field based on Compaign History Status and Responded flag

Hi Friends, I have a requirement like this...

In Lead standard object I have created one custom field called Compaign Name. I want to populate this field based on compaign history field called Status and Start date fields. Based on below criteria it would have populate...

If compaign history contains morethan one record then  based on most recent Date (Consider Start Date field) record compaign Name should populate on Lead object custom field. I am struggling with this requirement ...please help me.

PFA -- Screeshot

 Screenshot
Ashish_SFDCAshish_SFDC
Hi Kiru, 


This is not possible through a work flow as you have to compare and update the value present in a Look up related Object Field into the other Object. 

You would need to write a after update / insert trigger which will loop in the list of records in the related list and return the value if the recent record. 


Regards,
Ashish
Kiru535Kiru535
Yes...I know I have implemented trigger ...but I am getting error( Line 18 for loop)...Please help me on this trigger ASAP.

My code:

public class LeadfieldUpdate
{
  public void UpdateLead(CampaignMember[] cmember)
  {
    Set<Id> leadIds = new Set<Id>();
    Set<String> campaignperLead;
    List<Lead> leadforupdate = new List<Lead>();
    String Compname;
   
    for (CampaignMember  cm: cmember)
    {
       leadIds.add(cm.LeadId);
    }
    for(Lead c:[SELECT id, Compaign_Name__c,(SELECT id, CampaignId, Campaign.Name, LeadId, HasResponded FROM CampaignMembers) FROM Lead where Id IN :leadIds])
    {
      Compname = NULL;
      campaignperLead = new Set<String>();
      for(CampaignMember uis: c.CampaignMember)
      {
        IF(uis.Campaign.Name!= NULL && uis.Campaign.Name.length()>0)
        {
          Compname = Compname + uis.Campaign.Name;
        }
       
      }
      boolean isUpdateRequired = false;
      if(c.Compaign_Name__c != string.valueof(Compname))
      {
        c.Compaign_Name__c = string.valueof(Compname);
        isUpdateRequired = true;
      }
    }
  }
}
Ashish_SFDCAshish_SFDC
Hi , 


What is the error that you are getting?

Enable debug logs and see the logs if they leave any clue. 

Share the error and logs if possible. 


Regards,
Ashish