1

Can I draw a Picklist in a Visualforce with a Javascript query?

With de ajax connection.js in visualforce, I'm trying to retrieve a list of some values, Ex:

var qr = sforce.connection.query(“SELECT Name FROM EmailTemplate WHERE Folder.DeveloperName ='”+ MyCustomTemplates +”‘”);

But I have no Idea how to draw the result in a Visualforce Picklist.

Any idea?

Thanks!!!

  • 2
    Why not just use a controller on your page? – Adrian Larson Apr 27 '16 at 16:30
  • 2
    You can add the select and option HTML elements to your page using JavaScript. But when you save the page there will be no automatic binding to properties on the controller. As Adrian suggests, building a list of SelectOption in a controller and using apex:selectList and apex:selectOptions will be simpler. – Keith C Apr 27 '16 at 17:03
  • Professional Edition :( – Raspi Apr 28 '16 at 8:21
2

You could create select within JavaScript

<apex:page >
<script type="text/javascript">
    var __sfdcSessionId = '{!GETSESSIONID()}';
</script>
<script src="../../soap/ajax/36.0/connection.js" type="text/javascript"/>
<script type="text/javascript">     window.onload = setupPage;
function setupPage() {
    var queryResult = sforce.connection.query("Select Id, Name From Account limit 5");
    var array = queryResult.getArray('records');

    var myDiv = document.getElementById("selectOption");
    var selectList = document.createElement("select");
    selectList.id = "mySelect";
    selectList.onchange = myFunction;
    myDiv.appendChild(selectList);

    for (var i = 0; i < array.length; i++) {
        var option = document.createElement("option");
        option.value = array[i].Id;
        option.text = array[i].Name;
        selectList.appendChild(option);
    }
}
function myFunction() {
    var x = document.getElementById("mySelect").value;
    document.getElementById("choice").innerHTML = "You selected: " + x;
}
</script>

<div id="selectOption"> </div>
<p id="choice"></p> 
</apex:page>
  • Perfect! Now I just need save with a commandButton action save the value. Thanks! – Raspi Apr 28 '16 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.