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
margotqmargotq 

Trouble checking session ID for subsequent calls

My coding is a bit rusty, so please forgive any blatent errors.  I am trying to create a function that checks if a session ID has already been set, and if not, invokes the login() function:
 

public bool loginCheck()
{
  sfbinding.SessionHeaderValue = new SessionHeader();
  string sId = sfbinding.SessionHeaderValue.sessionId;
  if (sId == "")
    return login();
else
   
return true;
}

If I call loginCheck() directly from a script, I get the desired result (always returns true because if the session ID is empty, then the login function is invoked).  However, if I call it from within another function, for example:

public string testFunction()
{
  if ( loginCheck() )
  {
    ...
  }
}

Then I get the error "UNKNOWN_EXCEPTION: Destination URL not reset. The URL returned from login must be set in the SforceService".  Can someone please help me understand why this is?

Thank you.

margotqmargotq
No need for help...I figured out the problem:
 
   if (sId == "")
 
Needed to be:
 
   if (sId == null)
SuperfellSuperfell
As you're calling
sfbinding.SessionHeaderValue = new SessionHeader();
every time, this is still not going to do what you want, as it'll do a login every single time because you're clearing out the session header.
margotqmargotq

Thanks Simon.

However, because I originally call [sfbinding.SessionHeaderValue = new SessionHeader();] within the login() function, if I try to remove that line from loginCheck() then I get this error:

Object reference not set to an instance of an object.

Every piece of sample code for logging in invokes [sfbinding.SessionHeaderValue = new SessionHeader();] within the login() function.  So...to my original question, if the SessionHeader is not set in the first place, how do I check if I need to do a login or not?

In other words, how have other people implemented a check to see if there is a need to perform a login?

Message Edited by margotq on 11-13-2006 05:59 PM

SuperfellSuperfell
Well, yeah, you would that's the standard error for trying to dereference null. Just check if the SessionHeaderValue is null before trying to get the sessionId property from it.