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
StatBoyAStatBoyA 

S-Control "Invalid_session_id"

To All,

Well I've looked at MANY of the already existing messages about the "invalid_session_id" error arising during attempts to use an S-Control, but don't THINK that any have really FULLY addressed my situation.

As an example, I've created a very simple HTML S-Control definition that recreates the "invalid_session_id" error:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
   <head>
      <title>Create Household S-Control</title>
      <script language="javascript"
              src="/soap/ajax/13.0/connection.js"
              type="text/javascript"></script>
      <script language="javascript"
              type="text/javascript">
<!--
function initPage() {
   //Tests S-control function by attempting to create new account rec

   alert("running initPage function");

   //Calculate name value
   var nameval = "{!Contact.FirstName} {!Contact.LastName}"
                 + " Test Account";
   alert("calculated name: " + nameval);

   var obj = new sforce.SObject("Account");
   alert("past creation of javascript SObject");
   alert("created SObject of type: " + obj.type);
   obj.Name = nameval;
   //alert("finished assigning values to javascript Sbject");

   //create db rec using SObject
   try {
      var saveResult = sforce.connection.create([obj]);
      alert("successfully created db record");
   }
   catch(error) {
      alert("Error creating db rec: (" + error.faultcode + ") "
            + error.faultstring);
   }

}
//-->
      </script>
   </head>
   <body onload="initPage()">
      <div id="js_output">
      </div>
   </body>
</html>

I've defined a detail button on the main "Contacts" layout to trigger this S-Control.  I'm running it with a profile of "system administrator", and with the "API Access" setting in place.  I've also double-checked settings of the sharing rules in effect for this instance of SalesForce to ensure both read and write privileges are avalable.  I'm using a SalesForce instance established using the non-profit template from just about 5 months ago. 

When the S-Control is executed, all goes well until the attempted "sforce.connection.create".  At this point, the "catch" block is triggered and the alert message displaying the error message is displayed.

I'll anxiously away the valuable advice/thoughts of this community...   

Thanks MUCH.

Larry
Best Answer chosen by Admin (Salesforce Developers) 
Philip_FPhilip_F

For anyone who comes across this thread, I was able to solve this issue for myself using both werewolf's cookie solution and dkador's merge variable solution.

 

I opted for dkador's recommendation because it seemed very clean and worked well for me:

 

sforce.connection.sessionId = "{!$Api.Session_ID}";

 

Here is my variation of werewolf's solution from http://community.salesforce.com/sforce/board/message?board.id=JAVA_development&message.id=5010#M5010 

 

 

//Setting the sessionId cookie

context.setCookie('sessionId', sforce.connection.sessionId, 1);

 

//Getting the sessionId cookie

sforce.connection.sessionId = context.getCookie('sessionId');

 

 Thanks to both werewolf and dkador for contributing!

 

All Answers

StatBoyAStatBoyA
One additon I neglected to mention, I have VERY similar S-controls running correctly in my "developer" account...

Larry
werewolfwerewolf
See my response here, it may solve your problem:

http://community.salesforce.com/sforce/board/message?board.id=JAVA_development&message.id=5010#M5010

Also, maybe try writing it in Visualforce if possible.  It would probably be quicker and easier.
StatBoyAStatBoyA
Werewolf,

So (LOL), I guess there was still something ELSE I forgot to mention.  This does NOT seem to be a case where the time limits set for a session have been exceeded.  I can log out totally, log in, then IMMEDIATELY generate the "invalid_session_id" error.  Since the session limit is set to 2 hours (I confirmed that), this behavior can't be explained as a valid lapse of a session over the time limit.

Thanks for the thoughts.  I'll follow up with some further thinking. 

(Got to confess, I'm only just beginning to feel like I'm getting my feet under me with conventional S-controls.  I sort of hate to move to some other technology before I even have THAT one polished up... but this wouldn't be the FIRST time... )

Again, thanks for the input.  I'll be sure to report back to this thread with results if I try ANYTHING revealing...


werewolfwerewolf
In my reply there I didn't say anything about the sessionId timing out.  I said that in some circumstances the AJAX toolkit doesn't retrieve the sessionId properly, and so you can use the method I suggested to set the sessionId explicitly so your AJAX toolkit can use it.
StatBoyAStatBoyA
Further report:

I just:

  1. created a brand new S-control,
  2. pasted in the code provided above,
  3. created a new "Cotnact" button to run that new S-control
  4. displayed the new button on the "Contacts" layout's "button/detail button" section

Now, the S-control works as expected.

PREVIOUSLY, I had simply pasted the code into the definition of the S-Control displayed by the button within in the "useful links/household links" section of the non-profit template's "contact" layout.  This resulted in the "invalid_session_id" error.

So now I'm both happy, but PUZZLED.  Happy that I've gotten an updated version of the nonprofit template's "create household for contact" S-control to run (the version delivered with the SF instance I'm working with used the beta 3.3 Ajax Toolkit.  But VERY puzzled about why the same code runs the way I just tried, but NOT the way I tried previously. 

If any guru here is willing to venture an explanation to me, I'd LOVE to understand this better without spending a LOT more time analyzing it MYSELF....  Werewolf?  Got an idea?
werewolfwerewolf
No idea.  Maybe something was cached funny in your browser, and the new Scontrol fixed it because it wasn’t cached?
StatBoyAStatBoyA
Werewolf,

I understand you said nothing about timing out, but thanks for making sure of my understanding...

I just HAPPENED to remember the other item I'd checked already while digesting your input...  Since I believe that had been mentioned in OTHER threads about getting "invalid_session_id" errors, I wanted to make sure I mentioned this as a situation that already seemed to be ruled out...

Again, I appreciate the input a LOT.
StatBoyAStatBoyA
Werewolf,

About "caching"... this is as good as ANY interpretation I can offer MYSELF right now. 

In fact, at THIS exact moment, I'm just drawing a complete blank trying to understand why the exact same code works fine in a NEW S-control...

Incidentally, I'd confirmed this result from a couple different computers, using several login sessions...  Does that sort of argue against a "caching" explanation? (NOT that I have any BETTER explanation...)
dkadordkador
Have you tried using the merge variable for s-controls?  Try setting your session ID explicitly by doing something like:

Code:
sforce.connection.sessionId = "{!$Api.Session_ID}";

 

StatBoyAStatBoyA
At this point, I have NOT tried anything like the merge field suggestion here...  The problem just doesn't seem to be recurring now. 

LOL, I'd still like to understand WHY this was happening, of course...
dkadordkador
We put a fix in place a while back to address this issue. Glad to hear it's been resolved for you.
Philip_FPhilip_F

For anyone who comes across this thread, I was able to solve this issue for myself using both werewolf's cookie solution and dkador's merge variable solution.

 

I opted for dkador's recommendation because it seemed very clean and worked well for me:

 

sforce.connection.sessionId = "{!$Api.Session_ID}";

 

Here is my variation of werewolf's solution from http://community.salesforce.com/sforce/board/message?board.id=JAVA_development&message.id=5010#M5010 

 

 

//Setting the sessionId cookie

context.setCookie('sessionId', sforce.connection.sessionId, 1);

 

//Getting the sessionId cookie

sforce.connection.sessionId = context.getCookie('sessionId');

 

 Thanks to both werewolf and dkador for contributing!

 

This was selected as the best answer