5

I am using the REST API for my application. But I also need to use Metadata API. So for Metadata API calls I am trying to use the Session ID of OAuth authentication. For now I am able to use the session id for SOAP api calls but when I call a metadata method I get this error: "INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session"

Here is my code:

try{
 $mySoapClient = $mySforceConnection->createConnection(SOAP_CLIENT_BASEDIR.'/partner.wsdl.xml');

  $serverUrl = "$instance_url/services/Soap/u/27.0/00Dxxxxxxxxxxxx";
  $sessionId = $_SESSION['access_token'];
  $mySforceConnection->setSessionHeader($sessionId);
  $mylogin = $mySforceConnection->attach($serverUrl, $sessionId);

  $myMetadataConnection = new    SforceMetadataClient(SOAP_CLIENT_BASEDIR.'/metadata.wsdl.xml', $myLogin, $mySforceConnection);

  /*--These 3 lines of code works fine ----*/

   $query = "SELECT Id, FirstName, LastName, Phone from Contact";

    $response = $mySforceConnection->query($query);
    print_r($response);

   /*--------------------------------------*/

    $customField = new SforceCustomField();

    $customField->setFullName('Contact.MyCustomFieldb__c');
    $customField->setDescription('Description of New Field');

   $customField->setLabel('My Custom Field Label');

   $customField->setType('Text');
   $customField->setFormula('HYPERLINK(Password__c , "The Name")');
     $customField->setLength(100);
  //Contact->nameField=$customField;

 /*-----------This line giving the Error----------*/
    print_r($myMetadataConnection->create($customField));

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

Code of attach method:

public function attach($url, $sid) {
   $this->sessionId = $sid;
   $this->setSessionHeader($this->sessionId);
   $serverURL = $url;
   $this->setEndPoint($serverURL);
}
  • Where are you getting the attach method? I don't see it in the PHP Toolkit. – metadaddy Aug 29 '13 at 15:10
  • @metadaddy I read a blog post where the writer used 'attach' method. On contacting him he provide me the PHP toolkit having 'attach' method. This is link to post. link – abdn Aug 30 '13 at 11:14
  • Could you post the attach code in the question? I don't see it anywhere online. – metadaddy Aug 30 '13 at 22:47
  • @metadaddy yes sure... I edit my question and include code for attach method in the question.. – abdn Aug 31 '13 at 15:55
2

You have a few errors in the above code:

  1. attach() does not return a LoginResult, so the SforceMetadataClient doesn't get a session id.
  2. The Metadata API has a different endpoint from the SOAP Partner API (m in the URL in place of u).
  3. You can't set a length on Formula Fields.

Making those corrections, your code should look like this (my changes marked out with *):

try{
    $mySoapClient = $mySforceConnection->createConnection(SOAP_CLIENT_BASEDIR.'/partner.wsdl.xml');

    $serverUrl = "$instance_url/services/Soap/u/27.0/00Dxxxxxxxxxxxx";
    $sessionId = $_SESSION['access_token'];
    $mySforceConnection->setSessionHeader($sessionId);
    $mySforceConnection->attach($serverUrl, $sessionId);

    // *** Metadata API is on its own endpoint
    $metadataServerUrl = "$instance_url/services/Soap/m/27.0/00Dxxxxxxxxxxxx";

    // *** Make an object with the fields that the SforceMetadataClient constructor expects
    $myLogin = new stdclass();
    $myLogin->sessionId = $sessionId;
    $myLogin->metadataServerUrl = $metadataServerUrl;

    $myMetadataConnection = new    SforceMetadataClient(SOAP_CLIENT_BASEDIR.'/metadata.wsdl.xml', $myLogin, $mySforceConnection);

    /*--These 3 lines of code works fine ----*/

    $query = "SELECT Id, FirstName, LastName, Phone from Contact";

    $response = $mySforceConnection->query($query);
    print_r($response);

    /*--------------------------------------*/

    $customField = new SforceCustomField();

    $customField->setFullName('Contact.MyCustomFieldb__c');
    $customField->setDescription('Description of New Field');

    $customField->setLabel('My Custom Field Label');

    $customField->setType('Text');
    $customField->setFormula('HYPERLINK(Password__c , "The Name")');
    // *** Don't set length on a FormulaField
    // $customField->setLength(100);
    //Contact->nameField=$customField;

    /*-----------This line giving the Error----------*/
    print_r($myMetadataConnection->create($customField))
} catch (Exception $e) {
    echo $myMetadataConnection->getLastRequest();
    echo $e->faultstring;  
}

Output:

stdClass Object
(
    [result] => stdClass Object
        (
            [done] => 
            [id] => 04sE0000002GfQ0IAK
            [numberComponentErrors] => 0
            [numberComponentsDeployed] => 0
            [numberComponentsTotal] => 0
            [numberTestErrors] => 0
            [numberTestsCompleted] => 0
            [numberTestsTotal] => 0
            [state] => InProgress
        )

)
  • Thank you @metadaddy. It worked fine and solved lot of my problems. :) – abdn Sep 4 '13 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.