Get Selected Records from ListView in Visualforce Page | GETRECORDIDS JavaScript | StandardSetController Salesforce

In this article, you will learn to use GETRECORDIDS function to get selected records IDs in Visualforce page.

Requirement - Convert onclick JavaScript on custom buttons to Visualforce page

The requirement was to convert onclick JavaScript on custom buttons to Visualforce page. In my scenario, I am having a listview of accounts with checkbox. There is a custom button available on which there is some JavaScript function to follow the selected accounts.

dont miss out iconDon't forget to check out: Creating Wrapper Class in Salesforce Visualforce Pages

In my Visualforce page I was able to call the JavaScript on page load but I was unable to get the selected Account IDs.
 
I found a workaround for that using GETRECORDIDS.
 
In JavaScript you can easily get selected records using GETRECCORDIDS, i.e for Account you can use {!GETRECORDIDS($ObjectType.Account)};
 
But in Visualforce page we have to get the IDs using StandardSetController.
 
Please follow the below code to get selected records IDs from the list view to VisualForce page.

Visualforce Page

<apex:page standardController="Account" recordSetVar="accs" extensions="JavaScript_DemoV1Ctrl">
<apex:includeScript value="/soap/ajax/18.0/connection.js"/>
    <script>
       var GetIDs= '{!accIds}';
    </script>
</apex:page>

Apex Controller

public class JavaScript_DemoV1Ctrl {
    public List<Account> getIDs;
    public String accIds{get;set;}    
    public JavaScript_DemoV1Ctrl(ApexPages.StandardSetController controller){
        System.debug('Get Selected');
        getIDs = controller.getSelected();
        accIds = '';  
        for(Account acc : getIDs){
            accIds += acc.Id + ','; 
            System.debug('Account ID : '+accIds);
        }
        accIds = accIds.removeEnd(','); 
    } 
}

dont miss out iconCheck out another amazing blog by Kapil here: Sort Picklist Values in Lightning Component | Salesforce Tutorials

If you have any question please leave a comment below.
If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow!
 
Thanks
Keep Coding 

Responses

Popular Salesforce Blogs