+ Start a Discussion
PalakPalak 

SOQL in Javascript : Visualforce page

Hi all,

 

My aim is to use SOQL in javascript, for this reason I tried using the following code, but it gives session invalid message..

Please can amybody help me its urgent..

 

With Regards

Palak

 

<apex:page >
// InitPage Called by OnLoad.
<script language="JavaScript1.2" src="/js/functions.js"></script>
<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>
<script id="clientEventHandlersJS" language="javascript">
alert('check');
initPage();
function initPage() {
alert('test');
var existingSel = document.getElementById('select_0');
try
    {
    alert('Select Name, Id From Contact ORDER BY Name');
  var qr = sforce.connection.query("Select Name,Id From Contact ORDER BY Name") ;  
  alert('qr '+qr);
    }
catch (error)
    {
    alert(error.faultstring);
    }

if (qr.records.length > 0)
    {
    for (var i=0;i<qr.records.length;i++) 
        {
         existingSel.options[i] = new Option(qr.records[i].get("Name"), qr.records[i].get("Id"));
         }
    }
    else
    {
        alert("Query to populate picklist failed. No Rows.");
    }
// End Init Page with the bracket below.
}
</script>
<body onload="initPage();">

<select name="select_0" id="select_0" MULTIPLE="multiple" width="200" size="10">

<option value="">--None--</option>

</select>
</body>
</apex:page>
                               
 
Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code  snippet :

/ InitPage Called by OnLoad.

<script language="JavaScript1.2" src="/js/functions.js"></script>

<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>

<script id="clientEventHandlersJS" language="javascript">

alert('check');

initPage();

function initPage() {

alert('test');

var existingSel = document.getElementById('select_0');

sforce.connection.sessionId = '{!$Api.Session_ID}';

 

try

    {

    alert('Select Name, Id From Contact ORDER BY Name');

    var qr = sforce.connection.query("SELECT id,name FROM Contact");

    var records = qr.getArray("records");

  alert('qr '+qr);

    }

catch (error)

    {

    alert(error.faultstring);

    }

 

if (qr.records.length > 0)

    {

    for (var i=0;i<qr.records.length;i++)

        {

         existingSel.options[i] = new Option(qr.records[i].get("Name"), qr.records[i].get("Id"));

         }

    }

    else

    {

        alert("Query to populate picklist failed. No Rows.");

    }

// End Init Page with the bracket below.

}

</script>

<body onload="initPage();">

 

<select name="select_0" id="select_0" MULTIPLE="multiple" width="200" size="10">

 

<option value="">--None--</option>

 

</select>

</body>

</apex:page>

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

PalakPalak

Hi Prasanna,

 

Thanks for your reply. I tried to use it in the following way. But after adding the statement, javascript stopped working.

 

  sforce.connection.sessionId = “{!$Api.Session_ID}";
    alert('Select Name, Id From Contact ORDER BY Name');
  var qr = sforce.connection.query("Select Name,Id From Contact ORDER BY Name") ;  
  alert('qr '+qr);

 

With Regards

Palak Agarwal

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code  snippet :

/ InitPage Called by OnLoad.

<script language="JavaScript1.2" src="/js/functions.js"></script>

<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>

<script id="clientEventHandlersJS" language="javascript">

alert('check');

initPage();

function initPage() {

alert('test');

var existingSel = document.getElementById('select_0');

sforce.connection.sessionId = '{!$Api.Session_ID}';

 

try

    {

    alert('Select Name, Id From Contact ORDER BY Name');

    var qr = sforce.connection.query("SELECT id,name FROM Contact");

    var records = qr.getArray("records");

  alert('qr '+qr);

    }

catch (error)

    {

    alert(error.faultstring);

    }

 

if (qr.records.length > 0)

    {

    for (var i=0;i<qr.records.length;i++)

        {

         existingSel.options[i] = new Option(qr.records[i].get("Name"), qr.records[i].get("Id"));

         }

    }

    else

    {

        alert("Query to populate picklist failed. No Rows.");

    }

// End Init Page with the bracket below.

}

</script>

<body onload="initPage();">

 

<select name="select_0" id="select_0" MULTIPLE="multiple" width="200" size="10">

 

<option value="">--None--</option>

 

</select>

</body>

</apex:page>

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
PalakPalak

Hi Navatar,

 

Thanks for your help, but the multiselect picklist options were not getting populated. Can you please tell me why isnt getting populated;

 

With Regards

Palak Agarwal