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
SynchroSynchro 

Task Creation in PHP 5.1 - sanity check please

I need a sanity check... I'm trying to create a task from PHP 5.1 - I've got queries and general API calls working, I'm just unsure about the correct way of building a create request. I started with Zeev's outline, but it's giving some results I don't quite understand. Here's my creation function:

public function reportActivity($contactid, $perpid, $activitytype, $mailshotid = 0, $comment = '') {
switch($activitytype) {
case 'sentemail':
$task = array(
'WhoId' => $contactid,
'Description' => "Task done",
'OwnerId' => $perpid,
'ActivityDate' => gmdate('Y-m-d H:i:s')
);
$taskvar = new SoapVar((object)$task, SOAP_ENC_OBJECT, "Task", "http://soapinterop.org/xsd");
$res = $this->salesforce->create((object)array('sObjects' => $taskvar));
var_dump($this->salesforce->__getLastRequest());
break;
}
return false;
}

Here's an example SOAP request that it generates:





aBTPLIddpPQ.7YVyZ4rmuR22cABcdEZN6ApXDuU.yKne_GQEO0pRd9r5QzwU6cUp.A5oafVKfB6NKS_nTjNRhRhD2wYHcrlK4SNdGN37zi4=





00530000000go7eAAQ
Task done
00530000000go7eAAQ
2005-09-13 16:35:20





I have the feeling that my namespaces are not quite right, and the structure is not quite the same as the example SOAP create message in the docs - they seem to declare namespaces inline, rather than in the header section, though I don't know if that make any difference.

Comments?
SynchroSynchro
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://soapinterop.org/xsd" xmlns:ns2="urn:enterprise.soap.sforce.com" xmlns:ns3="enterprise.soap.sforce.com">
<SOAP-ENV:Header>
<ns3:SessionHeader>
<sessionId>aBTPLIddpPQ.7YVyZ4rmuR22cOKrysZN6ApXDuU.yKne_GQEO0pRd9r5QzwU6cUp.A5oafVKfB6NKS_nTjNRhRhD2wYHcrlK4SNdGN37zi4=</sessionId>
</ns3:SessionHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns2:create>
<ns2:sObjects xsi:type="ns1:Task">
<WhoId>00530000000go9MAAQ</WhoId>
<Description>Task done</Description>
<OwnerId>00530000000go9MAAQ</OwnerId>
<ActivityDate>2005-09-13 16:35:20</ActivityDate>
</ns2:sObjects>
</ns2:create>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SuperfellSuperfell
The fact that all the namespaces are declared in the root element doesn't matter. the namespace for the ns3 namespace is wrong (missing a urn: prefix)

the sessionId element should also be prefixed with ns2:
each of the fields elements (WhoId, Description etc) should be prefixed with ns3
the format of the date/time for activitydate is wrong, it should be in the form 2005-09-13T13:00:00Z (or someother TZ offset)
SynchroSynchro
Are any of these date formats acceptable? http://www.php.net/manual/en/ref.datetime.php#datetime.constants
Most of the ones that are in the same general shape as your example also contain a +0000 at the end. Will SF cope with that?

The docs say that the activitydate should always be in GMT so surely the TZ offset should always be 0? (I'm using gmdate anyway).

I need to do some more experimenting as most of the assignment of the different namespaces is handled by PHP, but I've fixed that missing 'urn'. I got the http://soapinterop.org/xsd namespace from Zeev's example, but I don't really understand why it's being used.
SynchroSynchro
I've stumbled across a little bit of magic. By changing the namespace in this line (which I found on the example SOAP messages):

$taskvar = new SoapVar((object)$task, SOAP_ENC_OBJECT, 'Task', 'urn:sobject.enterprise.soap.sforce.com');

It suddenly seems to line up with the WSDL, and produces a much more interesting SOAP request:

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="urn:sobject.enterprise.soap.sforce.com" xmlns:ns2="urn:enterprise.soap.sforce.com"><SOAP-ENV:Header><ns2:SessionHeader><ns2:sessionId>1mSjYHkeLFHpWnTEd8AJwkgbMpfcx5GrkUINRkXwtHD.WTgcGI4iCDGOGMPKFmv1vSmpUsHCw6NklmEyDuKozhhD2wYHcrlK4SNdGN37zi4=</ns2:sessionId></ns2:SessionHeader></SOAP-ENV:Header><SOAP-ENV:Body><ns2:create><ns2:sObjects xsi:type="ns1:Task"><fieldsToNull xsi:nil="1"/><Id xsi:nil="1"/><AccountId xsi:nil="1"/><ns1:ActivityDate>2005-09-14T12:18:34+0000</ns1:ActivityDate><CreatedById xsi:nil="1"/><CreatedDate xsi:nil="1"/><ns1escription>Task done</ns1escription><IsClosed xsi:nil="1"/><LastModifiedById xsi:nil="1"/><LastModifiedDate xsi:nil="1"/><ns1wnerId>00530000000go9MAAQ</ns1wnerId><Priority xsi:nil="1"/><Status xsi:nil="1"/><Subject xsi:nil="1"/><SystemModstamp xsi:nil="1"/><WhatId xsi:nil="1"/><ns1:WhoId>0033000000BvxhoAAB</ns1:WhoId></ns2:sObjects></ns2:create></SOAP-ENV:Body></SOAP-ENV:Envelope>

You can see that it's now assigning the correct namespace to the set fields, and automatically populating all the unset fields with the fact that they are nil. These nil fields are not being assigned the ns1 namespace, which doesn't look right to me - is that a PHP bug?
SynchroSynchro
This forum doesn't make it easy to post XML snippets... Trying again (ignore the smileys):

<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="urn:sobject.enterprise.soap.sforce.com"
xmlns:ns2="urn:enterprise.soap.sforce.com">
<SOAP-ENV:Header>
<ns2:SessionHeader>
<ns2:sessionId>1mSjYHkeLFHpWnTEd8AJwkgbMpfcx5GrkUINRkXwtHD.WTgcGI4iCDGOGMPKFmv1vSmpUsHCw6NklmEyDuKozhhD2wYHcrlK4SNdGN37zi4=</ns2:sessionId>
</ns2:SessionHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns2:create>
<ns2:sObjects xsi:type="ns1:Task">
<fieldsToNull xsi:nil="1" />
<Id xsi:nil="1" />
<AccountId xsi:nil="1" />
<ns1:ActivityDate>2005-09-14T12:18:34+0000</ns1:ActivityDate>
<CreatedById xsi:nil="1" />
<CreatedDate xsi:nil="1" />
<ns1escription>Sent email (Smartmessages mailshot id = 2122)</ns1escription>
<IsClosed xsi:nil="1" />
<LastModifiedById xsi:nil="1" />
<LastModifiedDate xsi:nil="1" />
<ns1wnerId>00530000000go9MAAQ</ns1wnerId>
<Priority xsi:nil="1" />
<Status xsi:nil="1" />
<Subject xsi:nil="1" />
<SystemModstamp xsi:nil="1" />
<WhatId xsi:nil="1" />
<ns1:WhoId>0033000000BvxhoAAB</ns1:WhoId>
</ns2:sObjects>
</ns2:create>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SynchroSynchro
Just so that anyone reading this is clear, this code is now working for me.
I'm successfully creating new items using this pattern.
Just because it's working doesn't mean it's right though - I'm just looking for some
reassurance that I'm doing it right rather than relying on salesforce's ability to cope
with iffy SOAP.
SuperfellSuperfell
Its very close, the elemenets that are tagged as nil are in the wrong namespace, also the nil attribute is not quite correct, the XML Schema spec requires it to be xsi:nil='true' not xsi:nil='1' (this is probably a PHP bug).