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

Display Lead and Activity History

I am trying to  display all the Leads and it's Activity History on my Campaign Members Custom Screen  but my code is showing only those Leads which hasActivity History associated and it's not showing the leads those don't have Activity History Logged.

if the Lead does not have any Activities associated, How to display Leads and Activity History?

Can anyone please tell me How can i modify my below code?

public with sharing class CampaignView {
public Campaign camp {get; set; }
    public List<MemberWrapper> lMemberWrappers {get; set;}
        public CampaignView(ApexPages.StandardController controller) {
        camp = (Campaign)controller.getRecord();
        lMemberWrappers = new List<MemberWrapper>();
                  for(Lead ld : CampaignMembers) {
             for(ActivityHistory ah: ld.ActivityHistories){
                lMemberWrappers.add(new MemberWrapper(ld.Phone, ah.Subject));
    private List<Lead> CampaignMembers;
    public List<Lead> getCampaignMembers() {
        CampaignMembers = [Select Id,Name, Phone,MobilePhone,Email, LastModifiedDate,(Select id, Campaign.Name,Contact.Phone,Lead.FirstName,Lead.LastName,Lead.Name,LeadID,ContactID,Lead.Phone,Lead.Email, Lastmodifieddate,Status,CampaignId,Campign_ID__c,Lead.MobilePhone  From CampaignMembers where CampaignId =:camp.Id  ),
        (Select Subject, Id,lastModifiedDate From ActivityHistories    order by LastModifiedDate desc LIMIT 1  )
        From Lead  where Id IN(select LeadId from campaignMember where campaignId =:camp.Id ) ];
        return CampaignMembers;
    public class MemberWrapper {
        public String Phone {get; set;}
            public String Subject{get;set;}
            public Datetime LastActivityHistory {get; set;}
        public MemberWrapper(String Phone,String Subject ) {
            this.Phone = Phone;
Best Answer chosen by Rajashri

I think the issue could be due to that you are adding the member wrapper only if activity history exists.
probably add below code in the for loop  for(Lead ld : CampaignMembers) 

if (ah.isEmpty()) {
    lMemberWrappers.add(new MemberWrapper(ld.Phone, ''));