Friday, 21 March 2014

An Example showing How to Make Synchronous calls with AJAX Toolkit

  1. <apex:page >  
  2. <script type="text/javascript">  
  3.         var __sfdcSessionId = '{!GETSESSIONID()}';  
  4.         </script>  
  5.     <script src="../../soap/ajax/29.0/connection.js" type="text/javascript"/>  
  6.   
  7.     <script>  
  8.   
  9.     window.onload = setupPage;  
  10.         function setupPage() {  
  11.             var output = document.getElementById("output");  
  12.             var startTime = new Date().getTime()  
  13.             try {  
  14.                 var queryResult = sforce.connection.query("Select Id, Name, Industry From Account order by Industry limit 30");  
  15.                 layoutResults(queryResult, output, startTime);  
  16.             } catch(error) {  
  17.                 queryFailed(error, output);  
  18.             }  
  19.         }  
  20.         function queryFailed(error, out) {  
  21.             out.innerHTML = "<font color=red>An error has occurred:</font> <p>" + error;  
  22.         }  
  23.         function layoutResults(queryResult, out, startTime) {  
  24.             var timeTaken = new Date().getTime() - startTime;  
  25.                 if (queryResult.size > 0) {  
  26.                 var output = "";  
  27.                 var records = queryResult.getArray('records');  
  28.                 for (var i = 0; i < records.length; i++) {  
  29.                     var account = records[i];  
  30.                     output += account.Id + " " + account.Name + " [Industry - "+ account.Industry + "]<BR>";  
  31.                 }  
  32.                     out.innerHTML = output + "<BR> query completed in: " + timeTaken + " ms.";  
  33.                 } else {  
  34.                     out.innerHTML = "No records matched.";  
  35.                 }  
  36.         }  
  37.     </script>  
  38. <div id="output"></div>  
  39. </apex:page>  



Quick Link to parent document:  Embedding API Calls in JavaScript

No comments:

Post a Comment

Thank you for visiting. Your comments are highly appreciated.