4

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


Page:

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

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;
        }
    }
}
2
  • Am I to assume you are building a String containing javascript code in APEX? Sure you don't just have a typo? What's the source look like when you go view source on the html page? Oct 11, 2012 at 1:34
  • @steven : I need the list as an array in javscript. There isn't a typo and the list is getting displayed but when I console it out, I get the exception. Oct 11, 2012 at 3:20

3 Answers 3

5

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.

Controller

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

VF Page

<apex:page controller="MyController">
  <script>
    var s = [];
    <apex:repeat value="{!stuff}" var="thing">
      s.push('{!JSENCODE(thing)}');
    </apex:repeat>    
  </script>
</apex:page>
0
1

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

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_json_overview.htm?SearchType=Stem

After converting into string then we can JSENENCODE .

Thanks

1
  • Tried it! Getting ILLEGAL exception when I console it out. Oct 11, 2012 at 3:13
0

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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .