I created Rest post method for inserting new lead record but in lead Sobject there is Campaign Lookup filed name is 'Campaign' but when i save this method i got the error "Error: Compile Error: Invalid field Campaign for SObject Lead at line 53 column 5"

 global static void CreateRecord()

    RestRequest req = RestContext.request;
    Blob body = req.requestBody;
    String bodyString = body.toString();

    //Deserializing the data sent into concrete SFDC objects
    LeadUtils jsonwrap =(LeadUtils)JSON.deserialize(bodyString,LeadUtils.class);

    //Initlize the lead object
    Lead ledID = new Lead();
    Campaign camp =new Campaign();
    camp =[select Name from Campaign where Name =:'SmartQuote'];
    //Get requested parameter one by one
    ledID.SQ_Account_ID__c = jsonwrap.AccountID;
    ledID.FirstName = jsonwrap.First_nm;    
    ledID.LastName = jsonwrap.Last_nm;
    ledID.Phone = jsonwrap.Phone;
    ledID.Campaign =camp.Name;
    ledID.Email = jsonwrap.Email_id;
    ledID.Company= jsonwrap.Company;

    // insert lead record in salesforce 
    insert ledID;
    RestContext.response.addHeader('Content-Type', 'application/json');
     // Return a single Lead as an example
     RestContext.response.responseBody = 
     Blob.valueOf(JSON.serialize((Lead)[Select ID from Lead where ID=:ledID.ID]));

1 Answer 1


This is a bit of SFDC convenience trickery for users of the Lead standard layout.

  1. While it is true there appears to be a standard field 'Campaign' as per the list of fields as shown in the Setup | Customize | Leads | Fields, the Campaign field on the page layout is a user convenience to associate a Lead with a Campaign. The field does not exist on the Lead SObject. You can verify this by inspecting the schema via SFDC Workbench or other tool (or you can see this in the SFDC Object Reference Guide)
  2. What actually happens behind the scenes of the standard page layout is that whatever you enter in the Campaign lookup field creates a CampaignMember object (that further confusingly, appears in the standard layout on the Campaign History related list)

So, your REST service needs to both create the Lead and create a CampaignMember object (a junction between Lead and Campaign)

From the SFDC Help doc at https://help.salesforce.com/HTViewHelpDoc?id=leads_fields.htm&language=en_US

Campaign: Name of the campaign responsible for generating the lead. Displays only when creating a new lead. Data entered in this field is stored in the Campaign History related list on the lead.

  • 2
    I love magic fields. Especially when they're not clearly documented and people have to keep asking questions about it.
    – sfdcfox
    Aug 19, 2014 at 15:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .