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
jamesob5jamesob5 

Salesforce enterprise API - can't pass campaign object when creating new lead

In my .net application I'm trying to attach a campaign to a new lead using the API. Creating new lead works fine when I'm not trying to pass a campaign to it. I'm trying to locate the campaign by id, load t into a campaign object, pass that campaign object to the lead and create lead. I do locate the campaign using the query but I may be missing a step. I get error: "Invalid foreign key relationship name Campaign".

 

Please help.

 

 

 The code


objSF.Url = l.serverUrl;
objSF.SessionHeaderValue = new SessionHeader();
objSF.SessionHeaderValue.sessionId = l.sessionId;
GetUserInfoResult userInfo = l.userInfo;

// Adding new lead to SalesForce.com //
sObject[] aLeads = new sObject[1];
Lead lead = new Lead();
lead.FirstName = FirstName.Length > 25 ? FirstName.Substring(0, 25) : FirstName;
lead.LastName = LastName.Length > 25 ? LastName.Substring(0, 25) : LastName;
lead.Company = CompanyName.Length > 50 ? CompanyName.Substring(0, 50) : CompanyName;
lead.Title = Position;
lead.State = StateAbbr;
lead.PostalCode = Zipcode;
lead.Phone = "NA";
lead.LeadSource = "Web";
if (CameFrom == "Demo") strLeadStatus = "New Lead";
lead.Status = strLeadStatus;
string strLeadSoureDetail = "Community";
if (CameFrom == "Demo") strLeadSoureDetail = "Online Demo";
lead.Lead_Source_Details__c = strLeadSoureDetail;
lead.Email = Email;
lead.Practice_Specialties__c = "Unknown";
lead.Num_Providers__c = 1;
objSF.Url = l.serverUrl;
objSF.SessionHeaderValue = new SessionHeader();
objSF.SessionHeaderValue.sessionId = l.sessionId;

// Adding campaign to the lead //
//Web - Community Campaign ID: 701400000009wgw
//Web - Online Demo Campaign IS: 701400000009kBS
string strCampID = "701400000009wgw";

if (CameFrom == "Demo") strCampID = "701400000009kBS";
QueryResult qr = new QueryResult();
objSF.QueryOptionsValue = new QueryOptions();
objSF.QueryOptionsValue.batchSize = 250;
objSF.QueryOptionsValue.batchSizeSpecified = true;
qr = objSF.query("select id, Name from Campaign where id='" + strCampID + "'");

if (qr.size > 0)
{
for (int i = 0; i < qr.records.Length; i++)
{
Campaign camp = (Campaign)qr.records[i];
lead.Campaign = camp;
}
}

// Community Section //
lead.First_name__c = FirstName.Length > 25 ? FirstName.Substring(0, 25) : FirstName;
lead.Last_Name__c = LastName.Length > 25 ? LastName.Substring(0, 25) : LastName;
lead.NGC_ProfileID__c = ProfileID.ToString();
lead.Username__c = UserName.Length > 50 ? UserName.Substring(0, 50) : UserName;
lead.Primary_Email__c = Email;
lead.Company_name__c = CompanyName.Length > 50 ? CompanyName.Substring(0, 50) : CompanyName;
lead.Industry__c = IndType.Length > 50 ? IndType.Substring(0, 50) : IndType;
if (PositionDesc != null)
{
lead.Position__c = PositionDesc.Length > 30 ? PositionDesc.Substring(0, 30) : PositionDesc;
}
lead.Created_Date__c = DateTime.Now;
aLeads[0] = lead;

SaveResult[] sr = objSF.create(aLeads);

 

 

werewolfwerewolf
Lead does not have a field called Campaign, as you can see by browsing the schema using Apex Explorer or the Force.com IDE.  To associate a lead to a campaign, you must create a CampaignMember object which links the two together.
werewolfwerewolf
Correction: Lead does have a field called Campaign, but it's not available to the API and it's only really intended for Web To Lead.
elcid2000elcid2000

Workaround we used was to add a custom field of the campaign field we need on the lead (e.g. Name) and then have a trigger on campaign member object that populates the custom field. 

 

At least users can create views on the lead home page filtered with campaign name.

khayum33khayum33

Hi James,

 

I was wondering if you were able to get this resolved? I also need to associate a new lead with a specific campaign. We have 200+ report downloads and really need to know the specifics (report) of our web-to-lead...

 

Any guidance with this is greatly appreciated!

 

Kind regards,

Karen

Raymond LaTulippeRaymond LaTulippe
Create CampaignMember

In the Request Body, you put the id's that tie together.
https://developer.salesforce.com/docs/api-explorer/sobject/CampaignMember/post-campaignmember/try
Example:

{
  "CampaignId": "701f40000012I8cAAE",
  "LeadId": "00Qf4000007wYhVEAU"
}