Find your content:

Search form

You are here

Passing list from Controller to VF page


I'm trying to pass a list from controller to page and use it in Javascript as an array to form html content.

But, when I pass a list of objects, an exception is thrown. Uncaught Syntax error : Unexpected token : which clearly suggests that I need to encode my data like this.

var list = {!JSENCODE(listc)};

But, I get the following error. Incorrect argument type for function JSENCODE()

Am I missing something here? I need to parse the list (Javascript array).

Edit: Code sample updated


<apex:page controller="example">
  <apex:form id="fid">
    var s ={!JSENCODE(JSONString)};

Controller :

public class example{
    public list slist{get;set;} 
    public String JSONString{get;set;}
    public example(){
        slist = new List();
        slist.add(new innerclass(1));
        slist.add(new innerclass(2));
        slist.add(new innerclass(3));   
        JSONString = JSON.serialize(slist);             

    public class innerclass{
        public integer count;
        public innerclass(integer c){
            count = c;

Attribution to: Vignesh Damodharan

Possible Suggestion/Solution #1

Hi Mohith Shrivastava,

But when we use JSON string in javascript using JSENCODE then it will give javascript syntax error beacuse it will convert all new line to \n and add escape char before ".

PLease refer given below string

{\n \"header\" : \"Record ID\",\n \"width\" : 250,\n \"sortable\" : true,\n \"hidden\" : true,\n \"dataIndex\" : \"Id\"\n}, {\n \"header\" : \"Case ID\",\n \"width\" : 250,\n \"sortable\" : true,\n \"dataIndex\" : \"Name\"\n}, {\n \"header\" : \"Process\",\n \"width\" : 250,\n \"sortable\" : true,\n \"dataIndex\" : \"Process__c\"\n}, {\n \"header\" : \"Work Type\",\n \"width\" : 250,\n \"sortable\" : true,\n \"dataIndex\" : \"Worktype__c\"\n}, {\n \"header\" : \"Document Type\",\n \"width\" : 250,\n \"sortable\" : true,\n \"dataIndex\" : \"Document_Type__c\"\n}, {\n \"header\" : \"Current State\",\n \"width\" : 250,\n \"sortable\" : true,\n \"dataIndex\" : \"Current_State__c\"\n}, {\n \"header\" : \"Location\",\n \"width\" : 250,\n \"sortable\" : true,\n \"dataIndex\" : \"Location__c\"\n}, {\n \"header\" : \"Created Date\",\n \"width\" : 250,\n \"sortable\" : true,\n \"dataIndex\" : \"CreatedDate\"\n}, {\n \"header\" : \"Tracker Id\",\n \"width\" : 250,\n \"sortable\" : true,\n \"hidden\" : true,\n \"dataIndex\" : \"Tracker_Id__c\"\n}

Attribution to: Mekyush Jariwala

Possible Suggestion/Solution #2

Directly passing of object value in JSENENCODE function is not allowed .It needs value in the form of text or string.

I guess we can convert the list of objects into JSON strings or JSON object using the JSON system classes salesforce provide .

JSON.serialise method must work in your case

After converting into string then we can JSENENCODE .


Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #3

There are a lot of ways to do this, but this one works pretty well. Have the array get filled out in a repeat tag. You'll want to add some safeguards to avoid injection attacks if this is from user input. Also take care with how you use re-renders as your variables will get reset if your script panel is re-rendered.

Depending on what you're using this for in the page there may be a more direct route to this using the standard visual force tag library.


public class MyController {
  public List<String> stuff { get; set; }
  public MyController() {
    stuff = new String[] { 'Hello', 'World!' };

VF Page

<apex:page controller="MyController">
    var s = [];
    <apex:repeat value="{!stuff}" var="thing">

Attribution to: Ralph Callaway
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content