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
ccracyccracy 

Invalid_Session_Id returned while invoking an SControl from the UI

I have writen an HTML SControl that has a javascript function with an SOQL to query a custom object. SControl has a button that invokes this javascript function on click. I have added this Scontrol as a Home Page component. When I click this button, I get an exception saying Invalid Session Id. What can be wrong here?? Please advice.

Thanks,
Rohit M.

SteveBowerSteveBower
Hi, it's far easier to help if you post some code.

The most recent versions of the Ajax toolkit don't require that you set a session id.  The previous 3.3Beta toolkit did.  Use the latest version if you're not already.

Best, Steve
ccracyccracy
Thanks for the reply, Steve. However, I dont quite understand the solution. Could you please elaborate more.
Also, please let me know the way to set the session id.

Attaching the code for reference.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>
        <script language="javascript">
            function getServerRecords()
            {
                var query = "Select Id From Contact";
                var queryResult;
                try
                {
                    queryResult = sforce.connection.query(query);
                }
                catch(err)
                {
                    alert("Error querying for Contact: " + err);
                }
            }
        </script>
    </head>
    <body>
        <input type="button" class="button" name="testButton" id="testButton" value="Test" onclick="getServerRecords();">
    </body>
</html>

Thanks,
Rohit M.

SteveBowerSteveBower
Hi Rohit,  Well, you're s-control doesn't really *do* much of anything, however as near as I can see it works fine, no errors, etc.   Some debugging tools are:

Firefox
The Firebug add-on
easiest to just throw "debugger;" into your code as the first executable like of code in the function.


In addition to doing the query in a try/catch block you should check the results of the query.  queryResult.success , etc.

-Steve
ccracyccracy
You are right Steve. This is the scale down version of the SControl that I would be using. This is just to make things really easy.

On clicking the button on the SControl, I get the following alert message:

falutcode: 'sf:INVALID_SESSION_ID', faultstring:'INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session', .....

You were mentioning about the way to include the session id in the older versions. Could you please let me know how could I explicitly include the Session ID while invoking the SControl?
CJagannathCJagannath
Rohit,
 
You can add the following line of code in your existing SControl
 
<script language="javascript"
type="text/javascript"></script>
 
Also add following
 
sforceClient.init("{!API.Session_ID}", "{!API.Partner_Server_URL_60}");        before you start the java script function "getServerRecords()"
 
Please let me know if this resolves your issue.
 
Thanks and regards,
Jagannath
ccracyccracy
Hey, thanks Jagannath. But, I get the same exception yet again. Just to recap, I have added this SControl as a Home Page Component. The javascript funtion is invoked when the button is clicked.

This is really strange behaviour. I have writen many SControls, but never never faced this before.

Can someone please help whats wrong here??
SteveBowerSteveBower
Jagannath's code is the way you used to have to do it in the Beta versions of the toolkit.

For the current versions (which is what you're using), Salesforce inserts a currently valid session id into the generated HTML.  If you do a "View Source" on the HTML  of your simple page when it is rendered, you should see the session id that's being used.

Is the message being supplied from Salesforce code, or from your catch statement?

Sorry, I'm at a loss. 

Check security changes, account changes, etc. but, if you're logged in, I think you should be getting valid sesion id's.   Log out, take a walk, log in again and re-try.  If that's doesn't work, call Salesforce and ask them to hit Ctrl-Alt-Delete and re-boot the entire system.   :-)

When you figure it out, I'm curious what it is.   Probably something trivial we've both overlooked  :-)

Best, Steve.

SteveBowerSteveBower
And, just to be perfectly clear, you've written this as an s-control, then created a custom link for it on the home page, and added that custom link to a home page component which is on the home page layout.  And the custom link is of type s-control with one of the window settings like "open in current window", etc.  Right?

-S

ccracyccracy
The exception message is indeed from Salesforce, Steve.

But, the way I have deployed the SControl is little different.

This is what has been done:

1. I have created an HTML SControl. It has a button. Onclik of the button I invoke a javascript function. This function has a SOQL query to get the Contacts.

2. Next I created a Home Page Component of type HTML Area. This is basically an IFrame with its src set to the SControl.
<IFRAME style="WIDTH: 50px; HEIGHT: 50px" src="/servlet/servlet.Integration?lid=01NT0000000Ch3T" frameBorder=0></IFRAME>

3. Next, I included this Home Page Component in the Home Page Layout. Son, now I can see the Button (defined in the SControl) on the Left Narrow panel of Salesforce.

Now when I click the Button, I get INVALID_SESSION_ID exception. Does this give any other clue??

Thanks,
Rohit M.

SteveBowerSteveBower
Oh.  Well, so what's happening is when salesforce includes your HTML it correctly creates the IFrame.  The Iframe correctly goes out and grabs the *source code* of your s-control and renders it.  So, you have the correct look and behavior, however, since Salesforce didn't generate the code, you don't have the javascript inserted code to insert the session id into your source code.  So, with no session id at all, the invalid message is correct.

However, the right way to do this isn't to try to figure out how to pass the session id down through the Iframe to the s-control, but rather to do it differently in the first place.

Why are you using the Iframe intermediary?  Why not just use a Custom Link for now?  I realize that you may be doing this just to get a Button on the left hand side instead of a Link, but may I suggest that until (and if) Salesforce enables the same "Buttons and Links" functionality that they have on other object in home page components, just live with the link.


Since you could write the S-control to take the Session Id from the Request URL if it were passed in, then you'd be tempted to find a way to do a :
<IFRAME style="WIDTH: 50px; HEIGHT: 50px" src="/servlet/servlet.Integration—lid=01NT0000000Ch3T&session_id={$Api.Session_Id}" frameBorder=0></IFRAM

sort of thing.  However, since the HTML you put in your html area is simply rendered, not processed first, you can't use any of the substitutions you'd like to pass on the URL for the S-control. You're sort of stuck.

So, I don't have an answer for you, although I see what you're trying to do and your error message is easily repeatable. 

Best of luck, I hope this helps.  Steve.



JT LovellJT Lovell

The problem is the iframe URL, you have to include &ic=1 at the end.  Here's a working iframe link that displays an s-control on the left-hand side of the home page (on the nav bar).  It also works on the wide right-hand side of the home page.

<IFRAME src="/servlet/servlet.Integration?lid=01N700000009DPd&ic=1" frameBorder=0 width=180> </IFRAME>

Note: &ic=1 will be translated to &amp;ic=1 (this is ok, and will work)

jt_at_appirio.com