Salesforce error: Element {}item invalid at this location

3.2k views Asked by At

i am using the below code to connect to salesforce using php

 require_once ('SforcePartnerClient.php');
require_once ('SforceHeaderOptions.php');  
require_once ('SforceMetadataClient.php'); 

$mySforceConnection = new SforcePartnerClient(); 
$mySforceConnection->createConnection("cniRegistration.wsdl");
$loginResult = $mySforceConnection->login("username", "password.token");

$queryOptions = new QueryOptions(200);

try {

  $sObject = new stdclass();
  $sObject->Name = 'Smith';
  $sObject->Phone = '510-555-5555';
  $sObject->fieldsToNull = NULL;


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

  $ids = array();
  foreach ($createResponse as $createResult) {
    print_r($createResult);
    array_push($ids, $createResult->id);
  }

} catch (Exception $e) {

  echo $e->faultstring;
}

But the above code is connect to salesforce database. But is not executing the create commands. it's giving me the below error message

Creating the following: Element {}item invalid at this location

can any one suggest me to overcome the above problem

2

There are 2 answers

0
ksugured On

MAK, in your sample code SessionHeader and Endpoint setup calls are missing

$mySforceConnection->setEndpoint($location);
$mySforceConnection->setSessionHeader($sessionId);

after setting up those, if you still see an issue, check the namespace urn

$mySforceConnection->getNamespace

It should match targetNamespace value in your wsdl

0
Nawshine On

the value of $mySforceConnection should point to the xml file of the partner.wsdl.xml. E.g $SoapClient = $sfdc->createConnection("soapclient/partner.wsdl.xml");

Try adding the snippet code below to reference the WSDL.

$sfdc = new SforcePartnerClient();
                // create a connection using the partner wsdl
                $SoapClient = $sfdc->createConnection("soapclient/partner.wsdl.xml");

                $loginResult = false;

                try {
                    // log in with username, password and security token if required
                    $loginResult = $sfdc->login($sfdcUsername, $sfdcPassword.$sfdcToken);
                } 
                catch (Exception $e) {
                    global $errors;
                    $errors = $e->faultstring;
                    echo "Fatal Login Error <b>" . $errors . "</b>";
                    die;
                }           
                // setup the SOAP client modify the headers
                $parsedURL = parse_url($sfdc->getLocation());
                define ("_SFDC_SERVER_", substr($parsedURL['host'],0,strpos($parsedURL['host'], '.')));
                define ("_SALESFORCE_URL_", "https://test.salesforce.com");
                define ("_WS_NAME_", "WebService_WDSL_Name_Here");
                define ("_WS_WSDL_", "soapclient/" . _WS_NAME_ . ".wsdl");
                define ("_WS_ENDPOINT_", 'https://' . _SFDC_SERVER_ . '.salesforce.com/services/wsdl/class/' . _WS_NAME_);
                define ("_WS_NAMESPACE_", 'http://soap.sforce.com/schemas/class/' . _WS_NAME_);

                $urlLink = '';
                try {
                    $client = new SoapClient(_WS_WSDL_);
                    $sforce_header = new SoapHeader(_WS_NAMESPACE_, "SessionHeader", array("sessionId" => $sfdc->getSessionId()));
                    $client->__setSoapHeaders(array($sforce_header));

                } catch ( Exception $e ) {
                    die( 'Error<br/>' . $e->__toString() );
                }

Please check the link on Tech Thought for more details on the error.