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
oorellanooorellano 

Cannot Access to Campaign Field from Lead Object

Hi all,

 

My problem seems to be very simple...

I want to make for example this query: [select CampaignID from Lead]

 

The problem is that I don't have the name for Campaign field, I tried with Campaign and CampaignID but didn't work.

From my org, field name appears as Campaign. And if I look at Field-Level Security appears as Visible.

From Eclipse I can't see that field. 

 

Any ideas?

 

Thanks,

Orlando

Message Edited by oorellano on 12-21-2009 11:47 AM
Best Answer chosen by Admin (Salesforce Developers) 
jkucerajkucera

Sure - that shouldn't be too tricky with visualforce & apex.  Assuming your VF field is called something like CampaignID__c, the query would look something like this:

 

List<CampaignMember> cms=[SELECT CampaignID, LeadID, Status FROM CampaignMember WHERE CampaignID =: CampaignID__c];

Set<ID> LeadIDs= new Set<ID>();

For (CampaignMember cm:cms){
if(LeadIDs.contains(cm.LeadID)==FALSE){
LeadIDs.add(cm.LeadID);
}
}
//Now you have a list of all the campaigns these leads are in.

 

 

All Answers

jkucerajkucera

The Campaign field on Lead is a funky field that is not accessible via SOQL.  It's primary purpose is for quick campaign member creation when a lead is created (typically via web to lead - pass the CampaignID to create the member).

 

What are you trying to do with that query?  You'd be better off querying campaign member for the CampaignID for the desired leads.

 

If a lead trigger, might be something like this:

 

 

List<CampaignMember> cms=[SELECT CampaignID, LeadID, Status FROM CampaignMember WHERE LeadID in : Trigger.new()]; Set<ID> CampaignIDs= new Set<ID>(); For (CampaignMember cm:cms){ if(CampaignIDs.contains(cm.CampaignID)==FALSE){ CampaignIDs.add(cm.CampaignID); } } //Now you have a list of all the campaigns these leads are in.

 

 

 

oorellanooorellano

Hi, thanks. I didn't know that.

 

I wrote that example to make it simply to understand.

 

Now, what I really need is this:

A user will enter a CampaignID from a lookup on my page.

Then, I'll need to list all Leads that has that CampaignID.

 

I can try thinking a solution now I have your example.

By the way, do you think is possible make what I'm trying to do?

 

Thanks

jkucerajkucera

Sure - that shouldn't be too tricky with visualforce & apex.  Assuming your VF field is called something like CampaignID__c, the query would look something like this:

 

List<CampaignMember> cms=[SELECT CampaignID, LeadID, Status FROM CampaignMember WHERE CampaignID =: CampaignID__c];

Set<ID> LeadIDs= new Set<ID>();

For (CampaignMember cm:cms){
if(LeadIDs.contains(cm.LeadID)==FALSE){
LeadIDs.add(cm.LeadID);
}
}
//Now you have a list of all the campaigns these leads are in.

 

 

This was selected as the best answer
oorellanooorellano

Thanks, you're rigth!

 

It make sense,

 

Thanks again,

Orlando

jkucerajkucera

btw-this will "break" for any campaigns with more than 1000 unique leadID's as a set can only hold 1000 items.  You might want to add in a check for the size of the Set and "Break" the loop if the set has 999 items in it.

 

There are a bunch of other posts on how to bulkify code on these boards if you run into any other problems.

oorellanooorellano

Yes, thanks.

 

I'm aware of that.

 

Thanks for your help!

Kyle PursellKyle Pursell
Hi jkucera,

Quick question on your code above.  If I want to include the VF field on my lead page for the purpose of reporting on leads with/without campaigns, how would I go about adding the code?

Thank you!
Kyle