2

I am implementing my own SOAP API with Salesforce. I am not using the Salesforce PHP Client add-on.

I am trying to create a new object, such as a Contact or Lead, via the API.

My code to POST the sessionId and new Contact information.

$soapVar =  array('parameters'=>array('sessionId' => 'sessionId', 'Contact' => array('Company' => 'Test', 'LastName' => 'Last Name')));
$client = new SoapClient("wsdl file", null);
$header = new SoapHeader("[serverUrl]", "AuthHeader", $soapVar, false)l
$client->__setLocation("[serverUrl]");
$result = __soapCall("create, $soapVar");
print_r($result;)

I get the following error: Uncaught SoapFault exception: [soapenv:Client] No operation available for request {urn:enterprise.soap.sforce.com}create

Where am I supposed to be posting to and what am I doing wrong?

When I login through SOAP to request a sessionId and the location, I get the sessionId and the serverUrl is https://xx.salesforce.com/services/Soap/m/27.0/xxxxx

I am using the Sandbox mode.

Thanks!

1
  • Are you able to include the resulting SOAP message in the question? The xx in your sub-domain should be one of the cs* instances for a sandbox. Mar 5, 2015 at 23:41

1 Answer 1

0

Not sure why you're not using the PHP Toolkit, but here's some PHP that creates a Contact.

First, with the Enterprise SOAP API

// Namespaces
$ns1 = "urn:sobject.enterprise.soap.sforce.com";
$ns2 = "urn:enterprise.soap.sforce.com";

$client = new SoapClient($enterpriseWsdlFile, array ( 
    'trace' => TRUE, // Useful for debugging!
    'features' => SOAP_SINGLE_ELEMENT_ARRAYS // Preserve your sanity
));

$header = new SoapHeader($ns2, "SessionHeader", array ('sessionId' => $sessionId));

// $enterpriseUrl has form https://na1.salesforce.com/services/Soap/c/33.0/[ORG_ID]
$client->__setLocation($enterpriseUrl);
$client->__setSoapHeaders($header);

$contact = new stdclass();
$contact->FirstName = 'Jane';
$contact->LastName = 'Doe';

// You can send multiple records in one call by just putting more
// entries in this array
$sObjects = array( 
    new SoapVar($contact, SOAP_ENC_OBJECT, "Contact", $ns1)
);

try {
    $result = $client->create(new SoapParam($sObjects, 'sObjects'));
    echo "Created records\n";
    foreach ($result->result as $value) {
        echo $value->id."\n";
    }

    // Debug
    echo "SOAP Client returned: \n";
    print_r($result);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Debug...
echo "SOAP Request: \n";
print_r($client->__getLastRequest());
echo "\n";
echo "SOAP Response: \n";
print_r($client->__getLastResponse());
echo "\n";

And the same thing, with the Partner SOAP API:

// Namespaces
$ns1 = "urn:sobject.partner.soap.sforce.com";
$ns2 = "urn:partner.soap.sforce.com";

$client = new SoapClient($partnerWsdlFile, array ( 
    'trace' => TRUE, // Useful for debugging!
    'features' => SOAP_SINGLE_ELEMENT_ARRAYS // Preserve your sanity
));

// $partnerUrl has form https://na1.salesforce.com/services/Soap/u/33.0/[ORG_ID]
$client->__setLocation($partnerUrl);

$header = new SoapHeader($ns2, "SessionHeader", array ('sessionId' => $sessionId));
$client->__setSoapHeaders($header);

$fields = array (
    new SoapVar('Contact', XSD_STRING, null, null, 'type', $ns1),
    new SoapVar('Jane', XSD_STRING, null, null, 'FirstName'),
    new SoapVar('Doe', XSD_STRING, null, null, 'LastName')
);

// You can send multiple records in one call by just putting more
// entries in this array
$sObjects = array( 
    new SoapVar($fields, SOAP_ENC_OBJECT, null, null) 
);

try {
    $result = $client->create(new SoapParam($sObjects, 'sObjects'));
    echo "Created records\n";
    foreach ($result->result as $value) {
        echo $value->id."\n";
    }

    // Debug
    echo "SOAP Client returned: \n";
    print_r($result);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Debug...
echo "SOAP Request: \n";
print_r($client->__getLastRequest());
echo "\n";
echo "SOAP Response: \n";
print_r($client->__getLastResponse());
echo "\n";
7
  • I'm getting closer! Only thing...I get an error on the line $result = $client->create(new SoapParam($sObjects, 'sObjects')); [sf:INVALID_TYPE] INVALID_TYPE: Must send a concrete entity type. I am using the Enterprise if it helps.
    – amlmf1
    Mar 6, 2015 at 15:10
  • Ah - my code is for the Partner API. I'll see if I can port it to Enterprise.
    – metadaddy
    Mar 6, 2015 at 21:15
  • Updated my answer with Enterprise API version
    – metadaddy
    Mar 6, 2015 at 22:14
  • 1
    Thank you so much. This helps a lot. I could not find anything in the docs. I went through the PHP client code but that was a little confusing.
    – amlmf1
    Mar 10, 2015 at 13:55
  • Yeah - the PHP SOAP client is pretty hairy.
    – metadaddy
    Mar 12, 2015 at 5:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .