function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
edoardo_spanoedoardo_spano 

Manage sObject list in Javascript

Hi All,

I need to manage a list of Accounts with Javascript, inside a Visualforce page.

I retrieved the accounts with a SOQL query and saved the result in a public List<Account>.
When I try to assign the list at a Javascript variable, the system retrieves this error message:

Uncaught SyntaxError: Unexpected token ILLEGAL

I just tried to assign the list in this way: var accounts = {!accns};

After this assignment, I need to read an information saved in a field for each account, but I don't know how I can access at that field.

Someone can help me about these issues?

Thanks in advance
Best Answer chosen by edoardo_spano
edoardo_spanoedoardo_spano
This is the solution that I used.

In visualforce:
<script>
var objList = jQuery.parseJSON('{!JSENCODE(objList)}');
</script>

In apex controller:
public class pagecontroller {

public String objList {get;set;}

/* wrapper object is not mandatory - you can use also a sObject list */
public class Object {
    public a {get;set;}
    public b {get;set;}
}

public void method() {
    List<Object> mylist = new List<Object>();
    .....
    mylist.add(/*something*/);
    .....
    objList = JSON.serialize(mylist);
}

}

Best regards!
 

All Answers

Ruwantha  LankathilakaRuwantha Lankathilaka
You can do this in folloiwing way,

<script>
     var jsAccounts= new Array();
     <apex:repeat value="{!accns}" var="accn">
       jsAccounts.push(accn);
     </apex:repeat>
  </script>



Then your jsAccounts array will have the account values. Now you can just loop through the array and access the field. Just try console.log(jsAccounts) to see the content of the account array. Then you will understand who you should retrieve the values
AK VineethAK Vineeth
.HI,

Please try to wrap your list variable in string format 

for example ,
var accountId='{!Account}';

Hope this helps you !!

Cheers 

 
AK VineethAK Vineeth
Here is the another version
<script> 
         var account= []; 
    </script> 
    <apex:repeat value="{!account}" var="acc"> 
        <script> 
            account.push('{!acc}'); 
        </script> 
    </apex:repeat>  
 
    <script>

in the above script we are defining an array ,and iterating over a list of account using repeat statment ,as per the javascirpt syntax inorder to push some records into array we use push method the same way we use in apex controller ,Ex:list.add(a); ,to check the ouptup use console.log('Account') , to see your ouptput .

Hope this helps you 

Cheer 

Ruwantha  LankathilakaRuwantha Lankathilaka
Eventhough I have not try , I assume there is another approch is to json serialize the list in the controller and then return the string into the view. You can  parse the string into json object in view. Then you can process the json object and do all the things.
edoardo_spanoedoardo_spano
This is the solution that I used.

In visualforce:
<script>
var objList = jQuery.parseJSON('{!JSENCODE(objList)}');
</script>

In apex controller:
public class pagecontroller {

public String objList {get;set;}

/* wrapper object is not mandatory - you can use also a sObject list */
public class Object {
    public a {get;set;}
    public b {get;set;}
}

public void method() {
    List<Object> mylist = new List<Object>();
    .....
    mylist.add(/*something*/);
    .....
    objList = JSON.serialize(mylist);
}

}

Best regards!
 
This was selected as the best answer