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
sgeninsgenin 

I can't create objects using the API

Hello,
I try to insert objects using the PHP API.
All the SELECT commands work, the CREATE commands return no error, but nothing is created.
Here is my code :

$client = new SalesforceClient($result->sessionId);
$client->_endpoint = $result->serverUrl;
$client->setOpt("timeout", 60);

$query_str = "select Id,description from lead where email='$email' and isConverted=False";

$result = $client->query($query_str);

if (PEAR::isError($result))
{
print "

".$result->getMessage()."
";
print "$query_str

";
$ret=0;
}
else
{
// No lead in the database
if (count($result->records) == 0)
{
$FIELDS=array(
'type' => 'lead',
'FirstName' => utf8_encode($firstname),
'LastName' => utf8_encode($lastname),
'Title' => utf8_encode($title),
'Company' => utf8_encode(strtoupper($company)),
'Website' => utf8_encode($url),
'Email' => utf8_encode($email),
'Phone' => utf8_encode($phone),
'Street' => utf8_encode($street),
'PostalCode' => utf8_encode($zip),
'City' => utf8_encode($city),
'Country' => utf8_encode($country),
'LeadSource' => utf8_encode($know_us),
'Recevoir_les_Infos_modifi_le__c' => $info_date,
'Recevoir_les_Infos_par_email__c' => utf8_encode($info_str),
'Recevoir_la_Newsletter__c' => utf8_encode($news_str),
'Recevoir_la_Newsletter_modifie_le__c' => $nl_date,
'Origine_du_contact__c' => utf8_encode($source));
$result=$client->create($FIELDS);

if (PEAR::isError($result))
{
print ("

".$result->getMessage()."
");
print_r ("$FIELDS

");
$ret=0;
}
}
}


At the end, I have no error, but nothing is inserted...
Can someone help me please?

Thanks
Stéphane
sgeninsgenin
The problem comes from two date fields. However, I can't understand which format I have to use. These two fields are date fields. The format I use is : YYYY-MM-DD. As soon as I try to set these fields, the CREATE action returns no error, but no lead is inserted.
Stéphane
rchoi21rchoi21
try the format:

2005-10-15T00:00:00.000+00:00

in PHP,

$startDate = date('Y-m-d\TH:i:s.000+00:00', mktime(0, 0, 0, 10, 15, 2005));
SuperfellSuperfell
Remeber that create/update return an array of SaveResult that indicate the outcome of each item in the call, you need to inspect that to find out why the create is being failed.
sgeninsgenin
Hello Simon,

I now check the value : $result->success, and the value is false.
However, I'm not an expert in PHP, and I can't find the way to retrieve the error message itself : I tried $result->errors->message, and $result->errors->message[0] and it doesn't work.

Thanks
Stéphane
sgeninsgenin
Thanks, but it still doesn't work.
The strange thing is that I have the same code source working on another server...
Stéphane
sgeninsgenin
Hi,

I managed to get the error code and message :

message=Recevoir les Infos modifié le: valeur de type non valide : 2005-10-15T00:00:00.000+00:00
statusCode=INVALID_TYPE_ON_FIELD_IN_RECORD

I also tried with 2005-10-15, and I get the same error.

In the WDSL, the field description is :


Thanks again for your help
Stéphane
SuperfellSuperfell
You're request is including a clashing type declaration, you might have the right format string, but the request is claiming its a different type, something like
<someDate xsi:type='xsi:String'>2005-10-10T11:11:11Z<someDate>

Even though you have the correctly formatted dateTime, this is claiming its a string not a dateTime, and so the server errors because its requiring a date (or dateTime).

You need to make sure the client either doesn't send the xsi:type attribute, or sends the correct value.
Park Walker (TAGL)Park Walker (TAGL)
For some reason that I haven't researched, there was a change in the PEAR SOAP library that causes this problem. I'm not sure in which version the change occured (I only have one client left using the PEAR library - everyone else is using PHP5). Anyway, the serializer is treating your Date as a String. You can force it to try converting it by adding:

$SOAP_RAW_CONVERT = true;

somewhere in your code: it just needs to be in the global name space.

This can cause other problems as the serializer will now attempt to convert every string to another data type: integer, binary (base64) or date_time. You can get around this by appending a single space to the end of string values that might contain [0-9]* or [a-zA-Z0-9]*.

There may be a better solution (other than switching to the native lib in PHP5) but this will work. The conversion code is in SOAP/BASE.php if you're interested. Using this method, gmdate("Y-m-d\TH:i:s",time()) will give you a date that the API will take.

Park
Park Walker (TAGL)Park Walker (TAGL)
Probably two different version of the PEAR SOAP library.
Park Walker (TAGL)Park Walker (TAGL)
If you are using the PEAR libraries try setting the global variable $SOAP_RAW_CONVERT = true; before you make the call.

At least some versions of the library us this flag in the SOAP/Base module to decide whether to attempt to parse doubles, base64 and date values, or leave them as strings.