+ Start a Discussion
RamyaKrishnaRamyaKrishna 

Reg: sforce.connection

Can any one explain me about sforce.connection in API calls.

Please send sample example to understand this?

I saw the document but not getting whole knowledge.

 

Thanks


Best Answer chosen by Admin (Salesforce Developers) 
ANKITAANKITA

Hi Ramya

<apex:page >
    <script type="text/javascript">
    var __sfdcSessionId = '{!GETSESSIONID()}';
    </script>
    <script src="../../soap/ajax/22.0/connection.js"
          type="text/javascript"></script>
    <script type="text/javascript">    
        window.onload = setupPage;
       
        function setupPage()
        {
                //function contains all code to execute after page is rendered 
                var state = {output : document.getElementById("output"),startTime : new Date().getTime()};
                var callback = {onSuccess: layoutResults, onFailure: queryFailed,source: state};
                sforce.connection.query("Select Id,Name from account",callback);
       }
      
      function queryFailed(error, source)
      {
            source.output.innerHTML = "An error has occurred: " + error;
      }
  function layoutResults(queryResult, source)
  {
            if (queryResult.size > 0)
            {   var output = "";
                var records = queryResult.getArray('records');
                    for (var i = 0; i < records.length; i++)
                    {
                        var account = records[i];
                        output += account.Name  + "<br>";
                    }
                //render the generated html string 
                source.output.innerHTML = output;
            }
  }
  </script>
    <div id="output"> </div>
</apex:page>

 

Just implement this in your VF page.

 

 

Thanks,

ANKITA

All Answers

Bramha1Bramha1
ANKITAANKITA

Hi Ramya

<apex:page >
    <script type="text/javascript">
    var __sfdcSessionId = '{!GETSESSIONID()}';
    </script>
    <script src="../../soap/ajax/22.0/connection.js"
          type="text/javascript"></script>
    <script type="text/javascript">    
        window.onload = setupPage;
       
        function setupPage()
        {
                //function contains all code to execute after page is rendered 
                var state = {output : document.getElementById("output"),startTime : new Date().getTime()};
                var callback = {onSuccess: layoutResults, onFailure: queryFailed,source: state};
                sforce.connection.query("Select Id,Name from account",callback);
       }
      
      function queryFailed(error, source)
      {
            source.output.innerHTML = "An error has occurred: " + error;
      }
  function layoutResults(queryResult, source)
  {
            if (queryResult.size > 0)
            {   var output = "";
                var records = queryResult.getArray('records');
                    for (var i = 0; i < records.length; i++)
                    {
                        var account = records[i];
                        output += account.Name  + "<br>";
                    }
                //render the generated html string 
                source.output.innerHTML = output;
            }
  }
  </script>
    <div id="output"> </div>
</apex:page>

 

Just implement this in your VF page.

 

 

Thanks,

ANKITA

This was selected as the best answer
Nazeer AhamedNazeer Ahamed

So - when we are using sforce.connection - is it API connection we are using?