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
RajashriRajashri 

Error: Invalid foreign key relationship: Lead.ActivityHistories

Hi,

I am trying to add the "Subject" Field for Lead from "Lead Activity History" table but i am getting an error that
"Invalid foreign key relationship: Lead.ActivityHistories" Can anyone please tell me how to add the Subject field in list.

Below is my code

public with sharing class singleListView {
    public Campaign camp {get; set; }
    public List<MemberWrapper> lMemberWrappers {get; set;}
        public singleListView(ApexPages.StandardController controller) {
        camp = (Campaign)controller.getRecord();
        lMemberWrappers = new List<MemberWrapper>();
        getCampaignMembers();
        getContactMembers();
        for(Lead ld : CampaignMembers) {
                    lMemberWrappers.add(new MemberWrapper(ld.Phone,ld.ActivityHistories.Subject));
                 
        }
        
    }
      private List<Lead> CampaignMembers;
    public List<Lead> getCampaignMembers() {
        CampaignMembers = [Select Id,Name, Phone,(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 and Status != '' ), 
        (Select Subject, Id,lastModifiedDate From ActivityHistories  where lastModifiedDate !=null and Subject !=null order by LastModifiedDate desc limit 1) 
        From Lead  where Id IN(select LeadId from campaignMember where campaignId =:camp.Id ) order by LastModifiedDate desc];
        return CampaignMembers;
    }     
         
   
    public class MemberWrapper {
               public String Phone {get; set;}
        public String Subject{get;set;}
        public MemberWrapper(String Phone,String Subject ) {
            this.Phone = Phone;
            this.Subject=Subject;
              }
       }
}
Best Answer chosen by Rajashri
Arunkumar RArunkumar R
In you for loop you can make the below changes to solve this issue,

Approach 1:
for(Lead ld : CampaignMembers) {
            
            for(ActivityHistory activityHis: ld.activityHistories)
            {
                   lMemberWrappers.add(new MemberWrapper(ld.Phone,activityHis.Subject));
            }
                 
        }
Approach 2:
for(Lead ld : CampaignMembers) {
            List<ActivityHistory> ah = ld.activityHistories;
            lMemberWrappers.add(new MemberWrapper(ld.Phone,ah[0].Subject));                
        }

Based on your scenario you can access activity history from the above two approaches.

All Answers

Arunkumar RArunkumar R
In you for loop you can make the below changes to solve this issue,

Approach 1:
for(Lead ld : CampaignMembers) {
            
            for(ActivityHistory activityHis: ld.activityHistories)
            {
                   lMemberWrappers.add(new MemberWrapper(ld.Phone,activityHis.Subject));
            }
                 
        }
Approach 2:
for(Lead ld : CampaignMembers) {
            List<ActivityHistory> ah = ld.activityHistories;
            lMemberWrappers.add(new MemberWrapper(ld.Phone,ah[0].Subject));                
        }

Based on your scenario you can access activity history from the above two approaches.
This was selected as the best answer
RajashriRajashri
Thanks Arun.I tried the first approach but it's not displaying the record when Subject Field in Activity History is null.

I want to display the records eventhough Subject Field is null.How can i do that?

 
Arunkumar RArunkumar R
Hi Rajashri,

In you SOQL Query you have checked the not null conditon, so remove the condition in your query, the modified query is
 
CampaignMembers = [Select Id,Name, Phone,(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 and Status != '' ), 
        (Select Subject, Id,lastModifiedDate From ActivityHistories  where lastModifiedDate !=null  order by LastModifiedDate desc limit 1) 
        From Lead  where Id IN(select LeadId from campaignMember where campaignId =:camp.Id ) order by LastModifiedDate desc];
RajashriRajashri
Thanks Arun.I tried that thing already but still it's displaying only those records for which activity History is assocaited.If Activity History is not present then it doesn't display that record..