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
Abhishek MahajanAbhishek Mahajan 

Print Individual records from a list using javascript

Hi,

I have a requirment where I want to print a every single record from a list using javascript.
Following is my conntroller and javaScript logic,

public class MyCompController
{
    public List<String> AccountIdList {get; set;}  
    public MyCompController()
    {
        List<Account> acc = [Select name, id from account];
        AccountIdList = new List<String>();
        for(Account a : acc)
        {
            AccountIdList.add(a.id);
        }   
    }
}

<script>
             var arr1 = new Array();      // OR var arr1 = [];       
             <apex:repeat value="{!AccountIdList}" var="accId">
                      arr1.push('{!accId}');
             </apex:repeat >                   
             alert('#### elements :' +arr1);       
</script>

Now I got list of accounts in arr1 array.
I want to print every single record of account , like (Name='abc', Id='a002800000NqbeB'), from this array in a table.

Please advice what needs to be done?
Thanks.
Best Answer chosen by Abhishek Mahajan
Sunil MadanaSunil Madana
Hi Abhishek, Please find the code below.

VF Code:
<apex:page controller="MyCompController">
    <apex:form>
        <apex:repeat value="{!AccountIdList}" var="accNum">
            <apex:outputText value="({!AccountIdList[accNum]}, {!AccountIdList[accNum].Name})" /><br/>
        </apex:repeat>
    </apex:form>
</apex:page>
Apex Class:
public with sharing class MyCompController
{    
    Map<Integer, Account> AccountIdList = new Map<Integer, Account>();

    public MyCompController() {
        Integer i = 0;
        for (Account a : [SELECT Id, Name FROM Account]) {
            AccountIdList.put(i, a);
            i++;
        }
    }

    public Map<Integer, Account> getAccountIdList() {
        return AccountIdList;
    }
}

Hope the above code helps, appreciate your reply if it works.

Thanks, Sunil.
 

All Answers

Sunil MadanaSunil Madana
Hi Abhishek, Please find the code below.

VF Code:
<apex:page controller="MyCompController">
    <apex:form>
        <apex:repeat value="{!AccountIdList}" var="accNum">
            <apex:outputText value="({!AccountIdList[accNum]}, {!AccountIdList[accNum].Name})" /><br/>
        </apex:repeat>
    </apex:form>
</apex:page>
Apex Class:
public with sharing class MyCompController
{    
    Map<Integer, Account> AccountIdList = new Map<Integer, Account>();

    public MyCompController() {
        Integer i = 0;
        for (Account a : [SELECT Id, Name FROM Account]) {
            AccountIdList.put(i, a);
            i++;
        }
    }

    public Map<Integer, Account> getAccountIdList() {
        return AccountIdList;
    }
}

Hope the above code helps, appreciate your reply if it works.

Thanks, Sunil.
 
This was selected as the best answer
Abhishek MahajanAbhishek Mahajan
Hi,

Thanks Sunil, it worked.