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
Harnoor Singh 9Harnoor Singh 9 

Query for related Lookups

I have a Lead with LastName as 'Lead'. It is added to a Campaign with name 'test'. The following Query isn't working.
SELECT Id, Campaign__c.Name from Lead where LastName='Lead'
Aparna Hegde 1Aparna Hegde 1
Hi Harnoor,

Campaign and lead are not directly related. They are related through campaign member object. You might have to tweak your SOQL like this -
SELECT LeadIdFROM CampaignMember WHERE CampaignId='XXX'
where XXX is the Id of your Campaign named Test

Cheers,
Aparna Hegde
 
Harnoor Singh 9Harnoor Singh 9
Hello Aparna,
Thank you for your help, it has been very useful. But what if i want to get Campaign Id using Lead Id?
Aparna Hegde 1Aparna Hegde 1
Hi Harnoor,
select campaignId,campaign.name from campaignmember where LeadId='XXX'
Since campaign object is campaign member's parent, you can get any field from campaign using campaign.fieldname in the above SOQL.

Cheers,
Aparna 
Harnoor Singh 9Harnoor Singh 9
Hello Aparna, Thank You. It works beautifully :) If I want to add a lead to campaign through a Lead Id, how about to do that? (campaign id and campaign Name are known to me)
Aparna Hegde 1Aparna Hegde 1
Hi Harnoor,

If you have a few of the leads, you can do it through UI. Go to Campaigns tab and add members. You can select the leads you want to add. If you have a lot then do it using data loader. You can make a csv file with Lead Id, and Campaign Id and all other details you have and execute through the same. You can also reach out to salesforce support to help you further on this.

Cheers,
Aparna Hegde