Requirement : 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 are some JavaScript function to follow the selected accounts.
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 below code to get selected records IDs from list view to VisualForce page.
Visualforce Page
1 2 3 4 5 6 | <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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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(','); } } |
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
0 Comments