+ Start a Discussion
noedskovnoedskov 

Linking a lead to a campaign

Hi all,

We use the API for various tasks and one of them is to create leads within salesforce which are generated from our website.

Now, a sub-set of these leads we want to make part of specific campaign and below I have included some example code.

Code:
String[] campaignIds = {"70120000000AFKy"}; //Campaign Id for Free Website Trial
SObject[] campaigns = stub.retrieve("Name", "Campaign ID", campaignIds); //Retrieve matching objects (should only be one)
Campaign campaign = null;
for(int x = 0; x < campaigns.length; x++) { //Loop through objects found
  campaign = (Campaign)campaigns[x];
  System.out.println("Got campaign: " + campaign.getName() + " with id: " + campaign.getId()); //Print name and id of campaign
}

No doubt I get the correct campaign but when I create a new lead and set the campaign (see code below) I get the following error message: Invalid foreign key relationship name Campaign

Code:
lead.setCampaign(campaign);

Any suggestions?

Cheers.

/Søren Nødskov Hansen
Best Answer chosen by Admin (Salesforce Developers) 
noedskovnoedskov
Simon,

Thanks for your input - it helped me a lot!

Just in case others are wondering how I ended up doing it here is that part of my code:

Code:
//Create a new lead
Lead lead = new Lead();
lead.setCompany("my company");
...
...

// Save the lead and verify result
Lead[] leads = {lead};
SaveResult[] results = stub.create(leads);
SaveResult result = null;
for(int i = 0; i < results.length; i++) {
  result = results[i];
  if(result.isSuccess()) {
    System.out.println("Lead created with id: " + result.getId());
                    
    CampaignMember member = new CampaignMember();
    member.setCampaignId("70120000000AFKy");
    member.setLeadId(result.getId());
                    
    // Save the member and verify result
    CampaignMember[] members = {member};
    SaveResult[] memberResults = stub.create(members);
    SaveResult memberResult = null;
    for(int j = 0; j < memberResults.length; j++) {
      memberResult = memberResults[j];
      if(memberResult.isSuccess()) {
        System.out.println("CampaignMember created with id: " + memberResult.getId());
      } else {
        System.out.println("Failed creating CampaignMember: " + memberResult.getErrors(j).getMessage());
      }
    }
  } else {
    System.out.println("Failed creating lead: " + result.getErrors(i).getMessage());
  }
}
 
It seemed that just setting the campaign id and the lead id on the campaign member was sufficient in order to map the two objects.

/Søren Nødskov Hansen

All Answers

SuperfellSuperfell
You need to create new CampaignMember records to link the campaign and the lead.
noedskovnoedskov
Simon,

Thanks for your input - it helped me a lot!

Just in case others are wondering how I ended up doing it here is that part of my code:

Code:
//Create a new lead
Lead lead = new Lead();
lead.setCompany("my company");
...
...

// Save the lead and verify result
Lead[] leads = {lead};
SaveResult[] results = stub.create(leads);
SaveResult result = null;
for(int i = 0; i < results.length; i++) {
  result = results[i];
  if(result.isSuccess()) {
    System.out.println("Lead created with id: " + result.getId());
                    
    CampaignMember member = new CampaignMember();
    member.setCampaignId("70120000000AFKy");
    member.setLeadId(result.getId());
                    
    // Save the member and verify result
    CampaignMember[] members = {member};
    SaveResult[] memberResults = stub.create(members);
    SaveResult memberResult = null;
    for(int j = 0; j < memberResults.length; j++) {
      memberResult = memberResults[j];
      if(memberResult.isSuccess()) {
        System.out.println("CampaignMember created with id: " + memberResult.getId());
      } else {
        System.out.println("Failed creating CampaignMember: " + memberResult.getErrors(j).getMessage());
      }
    }
  } else {
    System.out.println("Failed creating lead: " + result.getErrors(i).getMessage());
  }
}
 
It seemed that just setting the campaign id and the lead id on the campaign member was sufficient in order to map the two objects.

/Søren Nødskov Hansen
This was selected as the best answer