Represents a prospect or potential Opportunity.
create(), delete(), describeLayout(), describeSObjects(), getDeleted(), getUpdated(), merge(), query(), retrieve(), search(), undelete(), update(), upsert()
Leads have a special state to indicate that they have been converted into an Account, Contact, and optionally, an Opportunity. Your client application can convert leads via the convertLead() call. Users can also convert leads through the user interface. Once a lead has been converted, it is read-only. You can’t update or delete a converted lead. However, you can query converted lead records.
Leads have several fields that indicate their converted status. These special fields are set when converting the lead in the user interface.
Leads have a special state to indicate that they have not been viewed or edited by the lead owner. In the user interface, this is helpful for users to know which leads have been assigned to them but which they have not touched yet. IsUnreadByOwner is true if the lead owner has not yet viewed or edited the lead, and false if the lead owner has viewed or edited the lead at least once.
Each Status value corresponds to either a converted or unconverted status in the lead status picklist, as defined in the user interface. To obtain the lead status values in the picklist, a client application can query LeadStatus.
You can’t convert a lead via the API by changing Status to one of the converted lead status values. When you convert qualified leads into an account, contact, and opportunity, you can select one of the converted status types for the lead. Leads with a converted status type are no longer available in the Leads tab, although you can include them in reports.
To update a Lead or to convert one with convertLead(), your client application must log in with the “Edit” permission on leads.
When you create, update, or upsert a lead, your client application can have the lead automatically assigned to one or more User records based on assignment rules that have been configured in the user interface.
To use this feature, your client application needs to set either of the following options (but not both) in the AssignmentRuleHeader used in create or update:
The following Java sample shows how to automatically assign a newly created lead.
package com.doc.samples; import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import com.sforce.soap.enterprise.LoginResult; import com.sforce.soap.enterprise.QueryResult; import com.sforce.soap.enterprise.SaveResult; import com.sforce.soap.enterprise.SforceServiceLocator; import com.sforce.soap.enterprise.SoapBindingStub; import com.sforce.soap.enterprise._AssignmentRuleHeader; import com.sforce.soap.enterprise._SessionHeader; import com.sforce.soap.enterprise.fault.LoginFault; import com.sforce.soap.enterprise.fault.UnexpectedErrorFault; import com.sforce.soap.enterprise.sobject.Lead; import com.sforce.soap.enterprise.sobject.SObject; public class LeadAssignment { static LeadAssignment _leadAssignment; public static void main(String[] args) { _leadAssignment = new LeadAssignment(); try { _leadAssignment.CreateLead(); } catch (Exception e) { e.printStackTrace(); } } public void CreateLead() throws UnexpectedErrorFault, LoginFault, RemoteException, ServiceException { //Create the proxy binding and login SoapBindingStub binding = (SoapBindingStub) new SforceServiceLocator().getSoap(); LoginResult lr = binding.login("user@domain.net", "secret"); //Reset the binding to use the endpoint returned from login binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, lr.getServerUrl()); //Create the session id header, and add it to the proxy binding _SessionHeader sh = new _SessionHeader(); sh.setSessionId(lr.getSessionId()); binding.setHeader( new SforceServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh ); //Create a new case and assign various properties Lead lead = new Lead(); lead.setFirstName("Joe"); lead.setLastName("Smith"); lead.setCompany("ABC Corporation"); lead.setLeadSource("API"); //The lead assignment rule will assign any new leads that
//have "API" as the LeadSource to a particular user
//Create the assignment rule header and add it to the proxy binding _AssignmentRuleHeader arh = new _AssignmentRuleHeader(); //In this sample we will look for a particular rule and if found
//use the id for the lead assignment. If it is not found we will
//instruct the call to use the current default rule. You can't use
//both of these values together. QueryResult qr = binding.query("Select Id From AssignmentRule where Name = " + "'Mass Mail Campaign' and RuleType = 'leadAssignment'"); if (qr.getSize() == 0) { arh.setUseDefaultRule(new Boolean(true)); } else { arh.setAssignmentRuleId(qr.getRecords(0).getId()); } binding.setHeader( new SforceServiceLocator().getServiceName().getNamespaceURI(), "AssignmentRuleHeader", arh); // Every operation that results in a new or updated case, will
// use the specified rule until the header is removed from the
// proxy binding. SaveResult[] sr = binding.create(new SObject[] {lead}); for (int i=0;i<sr.length;i++) { if (sr[i].isSuccess()) { System.out.println("Successfully created lead with id of: " + sr[i].getId().getValue() + "."); } else { System.out.println("Error creating lead: " + sr[i].getErrors(0).getMessage()); } } // This call effectively removes the header, the next lead will
// be assigned to the default lead owner. Remember to add the
// session header back in. binding.clearHeaders(); binding.setHeader( new SforceServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh); } }
The following C# sample shows how to automatically assign a newly created lead.
using System;
using System.Collections.Generic;
using System.Text;
using LeadAssignment.sforce;
namespace LeadAssignment
{
class LeadAssignment
{
private SforceService binding;
private static readonly string Username = "ENTERUSERNAME";
private static readonly string Password = "ENTERPASSWORD";
/// <summary>
/// Create the proxy binding and login
/// </summary>
private LeadAssignment()
{
this.binding = new SforceService();
LoginResult lr = binding.login(LeadAssignment.Username, LeadAssignment.Password);
// Reset the binding to use the endpoint returned from login
this.binding.Url = lr.serverUrl;
// Create the session ID header and add it to the proxy binding
this.binding.SessionHeaderValue = new SessionHeader();
this.binding.SessionHeaderValue.sessionId = lr.sessionId;
}
[STAThread]
static void Main(string[] args)
{
LeadAssignment leadAssignment = new LeadAssignment();
try
{
leadAssignment.CreateLead();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Console.WriteLine(e.InnerException);
}
}
public void CreateLead()
{
// Create a new Lead and assign various properties
Lead lead = new Lead();
lead.FirstName = "John";
lead.LastName = "Brown";
lead.Company = "ABC Corporation";
lead.LeadSource = "Advertisement";
// Setting the lead source for a pre-existing lead assignment rule. This
// rule was created outside of this sample and will assign any new leads
// that have "Advertisement" as the LeadSource to a particular user
// Create the assignment rule header and add it to the proxy binding
AssignmentRuleHeader arh = new AssignmentRuleHeader();
// In this sample we will look for a particular rule and if found
// use the id for the lead assignment. If it is not found we will
// instruct the call to use the current default rule. Both these
// values can't be used together.
QueryResult qr = binding.query("Select Id from AssignmentRule where Name = " +
"'Mass Mail Campaign' and SobjectType = 'lead'");
if (qr.size == 0)
{
arh.useDefaultRule = true;
}
else
{
arh.assignmentRuleId = qr.records[0].Id;
}
binding.AssignmentRuleHeaderValue = arh;
// Every operation that results in a new or updated lead will use the
// specified rule until the header is removed from the proxy binding
SaveResult[] sr = binding.create(new sObject[] { lead });
foreach (SaveResult s in sr)
{
if (s.success)
{
Console.WriteLine("Successfully created Lead with ID: {0}", s.id);
}
else
{
Console.WriteLine("Error creating Lead: {0}", s.errors[0].message);
}
}
// This call effectively removes the header. The next lead will be assigned
// to the default lead owner.
binding.AssignmentRuleHeaderValue = null;
}
}
}