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
SampathNamburiSampathNamburi 

Closing or Reusing sobject connection

Hi,

 

  I have completed my coding part using sobjects where i have to create sobject connection for several times to the same custom object. I am interested to know if there is a way to reuse the same sobject connection as well as closing the sobject connection.

 

 I am using sobjects in my ajax coding and below is the some part of my code.

 

 

 var Tracking_Main = new sforce.SObject("CSP_Tracking_Main__c"); // creating sobject
 // if it is a fresh login then get cookie should return NULL
  Tracking_Main.Session_ID__c = __sfdcSessionId;
  Tracking_Main.User_ID__c = '{!$User.Id}';
 var result_Tracking_Main = sforce.connection.create([Tracking_Main], {
  onSuccess : success_Tracking_Main,
  onFailure : failure
  }
 );

 

 

 

 ------------------- after this i have to update the newly created record, for which i wrote as below---------------------------

 

  Tracking_Main = new sforce.SObject("CSP_Tracking_Main__c"); // recreae sobject 

  Tracking_Main.id = Get_Cookie("visitID");

Tracking_Main.Home__c = 1;

sforce.connection.update([Tracking_Main]); // update object

 ---------------------------------------------------------------------------------------------------------------------------------------------------------

 

 1. I would like to know on how to resue the same sobject that is created at the top

  2. How to close the sobject connection after my job is done.


 Any help in this regard is highly appreciated.

 

Thanks,

Sampath

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

1. You can theoretically use the same variable more than once (once onSucess is called, a results object is passed that gives you the newly created record's ID). Each call to sforce.connection methods, however, is still a discrete transaction (that is, once completed successfully, the data is committed and the connection is closed). There's no particular advantage to using either method in this scenario.

 

2. sforce.connection is simply a library function that makes calling the API easy. The point where onSuccess or onFailure is called, the connection has already been terminated (i.e. it does not use a KeepAlive), although a "session key" still exists so you can call more than one call without having to login. To render sforce.connection useless for the remainder of the page's life, just set sforce.connection.sessionId to a blank value; the API will no longer work until the page is reloaded or the sforce.connection.login call has successfully completed (thus generating a new "session key").