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
Dman100Dman100 

create lead thru API

I'm trying to create a lead thru the API from an asp.net web form.  I'm not getting any errors when I run the page, but the lead is not created.
 
Not sure what I'm missing.
 
Here is the basic implementation of the code:
 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using CompassLearning.Data.Models;
using CompassLearning.Data;
using sforce = SforceService;
public partial class Controls_SupportForm : System.Web.UI.UserControl
{
    SforceService sfdc;
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    private void login()
    {
        sfdc = new SforceService();
        LoginResult lr = sfdc.login("xxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxx");
        sfdc.Url = lr.serverUrl;
        sfdc.SessionHeaderValue = new SessionHeader();
        sfdc.SessionHeaderValue.sessionId = lr.sessionId;
        GetUserInfoResult userInfo = lr.userInfo;
    }
    private void createLead()
    {
        string phone = phoneAreaCode.Text + phonePrefix + phoneSuffix;
        Lead lead = new Lead();
        lead.FirstName = firstname.Text;
        lead.LastName = lastname.Text;
        lead.Street = address.Text;
        lead.City = city.Text;
        lead.State = state.SelectedValue;
        lead.PostalCode = zip.Text;
        lead.Phone = phone;
        lead.Email = email.Text;
        lead.Description = comment.Text;
        lead.LeadSource = "Web";
        sObject[] records = new sObject[] { lead };
        SaveResult[] sr = sfdc.create(records);
        for (int i = 0; i < sr.Length; i++)
        {
            if (sr[i].success)
            {
                Response.Write("Lead created successfully");
            }
            else
            {
                Error[] errors = sr[i].errors;
                for (int j = 0; j < errors.Length; j++)
                {
                    Response.Write(errors[j].message);
                }
            }
        }
    }
    protected void SubmitButton_Click(object sender, ImageClickEventArgs e)
    {
            login();
            createLead();
    }
   
}
 
Any help is appreciated.
Thanks.
Dman100Dman100

I figured out why the lead was not getting created.  I was missing a couple of required fields.  Once I included the required fields, the lead was successfully created.

I do have one follow-up question.  I have created a Lead Assignment Rule that will email the appropriate sales rep based on their territory.

Generating the Lead via the API doesn't seem to trigger the Assignment Rule?  How can I trigger the Assignment Rule when the lead is created thru the API?

Thanks.

BunnyBoyBunnyBoy
You'll probably want to take a look at the AssignmentRuleHeader section of the API documentation.  That describes how to trigger the assignment using your rule.
Dman100Dman100

I was reading about the AssignmentRuleHeader in the documentation and looking at the example, which was written in JAVA.  I am using C#.Net

private void createLead()
    {
        string phone = "(" + phoneAreaCode.Text + ") " + phonePrefix.Text + "-" + phoneSuffix.Text;

        Lead lead = new Lead();

        lead.FirstName = firstname.Text;
        lead.LastName = lastname.Text;
        lead.Street = address.Text;
        lead.City = city.Text;
        lead.State = state.SelectedValue;
        lead.PostalCode = zip.Text;
        lead.Phone = phone;
        lead.Email = email.Text;
        lead.Description = comment.Text;
        lead.LeadSource = "Web";
        lead.Status = "New";
        lead.RecordTypeId = "01250000000DJ8iAAG";
        lead.Company = "Unknown";

        AssignmentRuleHeader arh = new AssignmentRuleHeader();
        arh.assignmentRuleId = "01Q50000000A9iAEAS";

        sObject[] records = new sObject[] { lead };

        SaveResult[] sr = sfdc.create(records);
    }

The lead is created, but the assignment rule still isn't triggered?  What am I missing?

SuperfellSuperfell
try adding
arh.assignmentRuleIdSpecified = true;
Dman100Dman100

Hi Simon,

There isn't a definition for assignmentRuleIdSpecified for the AssignmentRuleHeader?

my full method is as follows:

private void createLead()

{

string phone = "(" + phoneAreaCode.Text + ") " + phonePrefix.Text + "-" + phoneSuffix.Text;

Lead lead = new Lead();

lead.FirstName = firstname.Text;

lead.LastName = lastname.Text;

lead.Street = address.Text;

lead.City = city.Text;

lead.State = state.SelectedValue;

lead.PostalCode = zip.Text;

lead.Phone = phone;

lead.Email = email.Text;

lead.Description = comment.Text;

lead.LeadSource = "Web";

lead.Status = "New";

lead.RecordTypeId = "01250000000DJ8iAAG";

lead.Company = "Unknown";

AssignmentRuleHeader arh = new AssignmentRuleHeader();

arh.assignmentRuleId = "01Q50000000A9iAEAS";

sObject[] records = new sObject[] { lead };

SaveResult[] sr = sfdc.create(records);

}

The Java example in the documentation shows re-setting the header?
 
binding.setHeader(new
SforceServiceLocator().getServiceName().getNamespaceURI(), "AssignmentRuleHeader",
   arh);
SuperfellSuperfell
Oh, you created the header object, but didn't set it on the client stub object.

sfdc.assignmentRuleHeaderValue = arh;
Dman100Dman100

Thanks Simon...the assignment rule is getting triggered now.

One thing I noticed is the email isn't generated when the assignment rule is triggered.  I double checked the assignment rule to verify I had selected an email template and that the template is available for use.  Everything looks okay.

When the rule is triggered it assigns the correct owner, but the email doesn't send?  I would figure if the owner is assigned that the email should fire as well.  Is there something additional that is required to fire the email?

SuperfellSuperfell
yes, see the EmailHeader.
Ricky MartinRicky Martin
Hi,
I am using above the code but I got follwoing error.


Line 1176:        [return: System.Xml.Serialization.XmlElementAttribute("result")]
Line 1177:        public SaveResult[] create([System.Xml.Serialization.XmlElementAttribute("sObjects")] sObject[] sObjects) {
Line 1178:            object[] results = this.Invoke("create", new object[] {
Line 1179:                        sObjects});

Line 1180:            return ((SaveResult[])(results[0]));

Serialization Error

My COde:
private void createLead()
    {
        Lead lead = new Lead();
        string phone = "(" + "123" + ") " + "123" + "-" + "123";
        lead.FirstName = "dfsd";
        lead.LastName = "dfsd";
        lead.Company = "Unknown";
        lead.Status = "Open - Not Contacted";
        lead.Street = "dfsd";
        lead.City = "dfsd";
        lead.State = "dfsd";
        lead.PostalCode = "dfsd";
        lead.Phone = phone;
        lead.Email = "rakishg@gmai.com";
        lead.Description = "dfsd";
        lead.LeadSource = "Web"; lead.Status = "New";
        lead.RecordTypeId = "01250000000DJ8iAAG";

sObject[] records = new sObject[] { lead };

SaveResult[] sr = service.create(records);

}


error in SaveResult[]

Thanks.