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
craskulcraskul 

Problem creating lead using API

Hello Everybody,

I'm pretty new to the API and I am having a problem with creating a lead.  It gives me the error of Required Fields Missing. Below the code, I'll paste in the responses I get. I've also pasted in the arrays and sobject to view although I took the print_r()'s out of the code below.

FYI: I don't want to use the web-to-lead because my next step is to add more functionality when capturing a lead. I thought I'd first start with getting a lead into the system first.


Here is my code (which is pretty much from one of the samples just changed to creating a lead vs. account:

Code ( I took out the login portion):
$fields = array (

'LastName' => $_POST['last_name'],
'Company' => $_POST['company'],
'Salutation' => $_POST['salutation'],
'FirstName' => $_POST['first_name'],
'Title' => $_POST['title'],
'Phone' => $_POST['phone'],
'Email' => $_POST['email'],
'LeadSource' => $_POST['lead_source']
);

$sObject = new SObject();
$sObject->type = 'Lead';
$sObject->fields = $fields;

echo "**** Creating the following:\r\n";
$createResponse = $mySforceConnection->create($sObject);
session_write_close();

} catch (Exception $e) {
echo $mySforceConnection->getLastRequest();
echo $e->faultstring;
}

 Responses:
This is the fields array
Array
(
[LastName] => Last123123
[Company] => testing
[Salutation] =>
[FirstName] => First123
[Title] => test
[Phone] => 949-555-1212
[Email] => test@tetst.com
[LeadSource] => Website
)

This is the sObject
SObject Object
(
[type] => Lead
[fields] => Array
(
[LastName] => Last123123
[Company] => testing
[Salutation] =>
[FirstName] => First123
[Title] => test
[Phone] => 949-555-1212
[Email] => test@tetst.com
[LeadSource] => Website
)

)
**** Creating the following:
stdClass Object
(
[errors] => stdClass Object
(
[fields] => Array
(
[0] => LastName
[1] => Company
)

[message] => Required fields are missing: [LastName, Company]
[statusCode] => REQUIRED_FIELD_MISSING
)

[id] =>
[success] =>
)

As you can see, the sObject Array has the fields in it. So I don't know how to solve this problem. Any help would be greatly appreciated.

Thanks,

Chris

tocktock
change the line:
 $createResponse = $mySforceConnection->create($sObject);
to
$createResponse     = $mySforceConnection->create(array($sObject));
 
 
and see if it works.
craskulcraskul
Thanks, that worked.

Chris
technoparkcorptechnoparkcorp
Looks like I have the same problem, but I use correct create() call.

This is my code:

        $Sforce = new SforceEnterpriseClient ();
        $mySoapClient = User::$Sforce->createConnection("sfa/enterprise.wsdl.xml");
        $mylogin = User::$Sforce->login(SFA_LOGIN, SFA_PASSWORD);
        $newLead = new SObject ();
        $newLead->fields = array (
                'LastName' => $lastname,
                'Company' => $company);
        $newLead->type = 'Lead';
        $response = $Sforce->create (array($newLead), "Lead");

And it does not work. The reply is the same as in the post above.

Any ideas?

tocktock
Hi,
 
 
   I think the line: 
$response = $Sforce->create (array($newLead), "Lead");
should be:
 $response = $Sforce->create (array($newLead));
 
you have already stated in your script that the code object you want to create is a lead.
Thanks,
James.
technoparkcorptechnoparkcorp
I already found the problem. It works with Partner, but doesn't work with Enterprise.

I just changed the WDSL.

I don't know why... Maybe some bug in the API.

Anyway, thanks.