[SalesForce] Call Login() to get a SessionID via sforce.connection.login()

In order to obtain a SessionID with maximal privileges I try to call login() via the API. I need that in order to ExecuteAnonymous with verbose Debug Log – see this post for Backgrounds: Get a FIRST-CLASS SessionID for API Calls (looking for a clean way or alternative)

At the question above @Eric suggested to use login() of the partner SOAP API in order to obtain a more powerful first-class SessionID with maximal privileges. I found that login() is supported by the JavaScript AJAX Toolkit. For just a test, it seems to be much easier using sforce.connection.login() then working with the WSDL.

What I did is very easy to reproduce: Having connection.js and apex.js already loaded, I simply went to a javascript console (firebug in my case) and entered

sforce.connection.login('username@sample.com','MyPassword'+'MySecurityToken');

Using proper username, password and token of course. It immediately produces a nice callout:

<se:Envelope xmlns:se="http://schemas.xmlsoap.org/soap/envelope/">
  <se:Header xmlns:sfns="urn:partner.soap.sforce.com">
    <sfns:SessionHeader>
      <sessionId>00D20000000D8Ia!ARIAQMHMuMKeHMW57dHfpayyxcTNKhdL46J3ZwgEn9BfeHyK0chN2IxTBrNtDAstB8lX9EynZuOa_BfIA9rgNHycd2Q7oYgA5</sessionId>
    </sfns:SessionHeader>
    <sfns:DebuggingHeader>
      <debugLevel>Detail</debugLevel>
    </sfns:DebuggingHeader>
  </se:Header>
  <se:Body>
    <login xmlns="urn:partner.soap.sforce.com" xmlns:ns1="sobject.partner.soap.sforce.com">
      <username>username@sample.com</username>
      <password>MyPasswordMyToken</password>
    </login>
   </se:Body>
</se:Envelope>

send to the endpoint https://elfcodefusion.eu0.visual.force.com/services/Soap/u/29.0

But the response ends with an error:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:fault.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <soapenv:Fault>
            <faultcode>sf:UNKNOWN_EXCEPTION</faultcode>
            <faultstring>UNKNOWN_EXCEPTION: An unexpected error occurred. Please include this ErrorId if you contact support: 702236781-42797 (-188291932)</faultstring>
            <detail>
                <sf:UnexpectedErrorFault xsi:type="sf:UnexpectedErrorFault">
                    <sf:exceptionCode>UNKNOWN_EXCEPTION</sf:exceptionCode>
                    <sf:exceptionMessage>An unexpected error occurred. Please include this ErrorId if you contact support: 702236781-42797 (-188291932)</sf:exceptionMessage>
                </sf:UnexpectedErrorFault>
            </detail>
        </soapenv:Fault>
    </soapenv:Body>
</soapenv:Envelope>

However

sforce.connection.logout();

works like a charm.

Any idea what's wrong with my callout?

Best Answer

I found it!

The problem was the presence of the debug headers, possibly also the already active (low power) sessionId. Removing both, it works great! In my case I just unset the properties in JavaScript just like so:

sforce.connection.sessionId = null;
sforce.connection.debuggingHeader = null; // this, too is necessary!

Then call again

sforce.connection.login('username@sample.com','MyPassword'+'MySecurityToken'); 

which produces a simpler XML and actually works :-)

<se:Envelope xmlns:se="http://schemas.xmlsoap.org/soap/envelope/">
   <se:Header xmlns:sfns="urn:partner.soap.sforce.com"></se:Header>
   <se:Body>
      <login xmlns="urn:partner.soap.sforce.com" xmlns:ns1="sobject.partner.soap.sforce.com">
         <username>username@sample.com</username>
         <password>MyPasswordMyToken</password>
       </login>
   </se:Body>
</se:Envelope>

Hope this will help someone!