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
ZitizonXZitizonX 

Session and SessionHeaderValue ASP.Net

Hi all,
 
Sorry about the previous post, it was a mistake. I am pretty new to salesforce and have a quick question about the "SessionHeaderValue".
 
Q. How can I make a SessionHeaderValue permenent for all API calls? Please have a look at the code below...
 
 
--------------------------------------------------------------

public SforceService binding = new SforceService();

void function1()

{

//execute the login placing the results
//in a LoginResult object
sforce.LoginResult loginResult = binding.login(userName,password);

//set the session id header for subsequent calls
binding.SessionHeaderValue = new sforce.SessionHeader();
binding.SessionHeaderValue.sessionId = loginResult.sessionId;

//reset the endpoint url to that returned from login
binding.Url = loginResult.serverUrl;

}

 

void function2()

{

if I want to do an API call here, it will through an NullRef Exception. Its because the SessionHeaderValue is empty or something to do with the Session Header. But I have already logged in to the system. For every API call do I have to write the SessionHeader ?

}

zero1578zero1578
Code:
private SforceService binding = new SforceService();

protected void page_load()
{
if(login())
{
function2();
}
}

private bool login()
{
LoginResult lr = binding.login("username", "xxxxx");
if (!lr.passwordExpired)
{
binding.Url = lr.serverUrl;
binding.SessionHeaderValue = new SessionHeader();
binding.SessionHeaderValue.sessionId = lr.sessionId;
GetUserInfoResult userInfo = lr.userInfo; //? to reference later if u want
return true;
}
else
{
Response.Write("Your password is expired");
return false;
}
}

private void function2()
{
your stuff here
}



 
I think you can store that information in Session variables in .net and just reference it when you need to.  If you are going from page to page, you would have to enable sessionstate to keep those variables.  If you are doing everything within one page, it should be keeping the values automatically. 
 
I used the code above recently in a single page webapp and it works great.  you would probably want to get rid of the hardcode username/pass , but we use API license for ours so it doesnt matter to us really.
 

 

Griffin WarburtGriffin Warburt

zero1578,

Thank you for the post!

I have been tasked with integrating our ERP system with SFDC.  Details are found in this post.

Your code has made me think that there is something I don't understand and I am hoping that you can help me with it.

How does your code example acquire the SessionID from SFDC?  Did you create a web-service to receive the response which contains the SessionID?  or will just using the WSDL as a web-reference (in Visual Studio) enable this communication?

 

Thanks!

zero1578zero1578
Adding the WSDL as a web reference, then include it in your source, and all the api calls open up to you.  The calls shown are available because of the WSDL and intellisense is fully functional, and it handles all the session pieces for you.  There is documentation luckily in case my descriptions or coding style is wrong, but I have built about 15 apps and all of them use that piece of code or very similar at some point, and they are used every day all day long.  If you cant get it working post your code and Ill compare it to what I use that seems to be working for us.  good luck.
Griffin WarburtGriffin Warburt

So by using the WSDL the API calls to SF return values, without a seperate Web-Service to handle the responses from SF.  That's fantastic!

I wish that were documented somewhere, although I expect that if I were more of a programmer I would have understood that already.

I'll post a follow-up if I have any problems and update my other posts, thank you for the assistance!

zero1578zero1578
try this http://www.salesforce.com/us/developer/docs/api/index.htm there are C# samples and Java samples I believe. good document other than the discrepancies between examples as they explain it vs examples from the Examples section. It covers everything you should need at least though. Also, I dont remember if its in the doc or not, but use Asynchronous calls if you can, its infinitely better with the way SF works and sends data to you.
Griffin WarburtGriffin Warburt

Synchronous—Once the API call is invoked, your client application waits until it receives a response from the service. Asynchronous calls are not supported.

 

That statement is in the Call Basics section.  Is there a way around that prohibition?

SuperfellSuperfell
The transport is synchronous. That doesn't mean your programming model has to be. If you're using .NET then it can generate you an asynchronous programming model, even though the underlying transport is synchronous.
zero1578zero1578
Code:
//this may not work for you but should work as a reference
//you use the built in Asynch calls in .net..there is a dev post around here somewhere about it
//***************
private void getCases(string who, string date)
 {
  if (date == "1-01-01")
  {
   Label1.Visible = true;
  }
  else
  {
   Label1.Visible = false;
   if (login())
   {
    if (who == "Customer Service")
    {
     prepareDataTable(who);

//THIS IS THE IMPORTANT PIECE
//**************************
     QueryResult qr = binding.query("SELECT Id,CaseNumber,Subject,OwnerId,ClosedDate,ContactId,Contact.Name,Contact.Last_Surveyed__c,Contact.Email FROM Case WHERE ClosedDate >= " + date + "T00:00:00Z AND RecordTypeId = '0123000000004NzAAI' AND (Contact_Type__c = 'Customer Contact' OR Contact_Type_1__c = 'Customer Contact' OR Contact_Type_2__c = 'Customer Contact' OR Contact_Type_3__c = 'Customer Contact') AND IsClosed = true AND Contact.DO_NOT_CONTACT__c = false ORDER BY Contact.Name");
     IAsyncResult asyncQueryMore = null;
     do
     {
      if (asyncQueryMore != null) qr = binding.EndqueryMore(asyncQueryMore);
      if (!qr.done) asyncQueryMore = binding.BeginqueryMore(qr.queryLocator, null, null);
      foreach (sforce.Case c in qr.records)
      {
       processCase(c, who);
      }
     }
     while (!qr.done);
//******** END IMPORTANT PIECE
    }
                          }
                      }
                  }




//It calls this method for me to store or process the values of each case it returns
//in this case i am adding the data to a data table so i can display it and manipulate it locally in a gridview


private void processCase(Case c, string who)
{
  DataRow row = dt.NewRow();
  if(who == "Customer Service")
  {
   
   row["Id"] = c.Id;
   row["Case Number"] = c.CaseNumber;
   row["Subject"] = c.Subject;
   row["ClosedDate"] = c.ClosedDate;
   row["OwnerId"] = c.OwnerId;
   row["ContactId"] = c.ContactId;
   row["Contact Name"] = c.Contact.Name;
   row["Contact Email"] = c.Contact.Email;
   row["LastSurveyDate"] = c.Contact.Last_Surveyed__c;
   dt.Rows.Add(row);
   
  }
}