Sunday 5 February 2012

How to get List view Id without open it in edit mode

Hi friends !  Before some days I stuck in strange problem where I needed id of any List view of any Sobject. For this when I select List view from List. URL does't get refresh so I can't get id of currently selected list-view.
I came with a new way to find id of selected list view in alert Box( or by passing it in URl to show on VF page ) . I have created a custom button 'Get Id of ListView' (Behavior : Execute JavaScript,Display Type: List Button) on S-object whose List view id I have to get and wrote a bit of on-click javascript code:
var locationOfcurrentPage = location.href ; 
locationOfcurrentPage = locationOfcurrentPage.substring(locationOfcurrentPage.lastIndexOf('=')+1 , locationOfcurrentPage.length) ; 
var selectedListViewId = locationOfcurrentPage+'_listSelect'; 
var selectedoptionId = document.getElementById(selectedListViewId).value; 

alert('Selected List view option Id is   ' + document.getElementById(selectedListViewId).value  );
As Below image shows clearly that id in Url is same as in alert box (which I get from JS code snippet)
But when I change list view from'Test view'  to 'All' and now I can clearly observe that url doesn't get refresh but in alert Box I can get id of currently selected list view option.
Now you can enjoy with getting current id every time without going to  Edit mode.
Where you can use this code:
1.) To show list view on VF Page.
2.) To redirect again to same page  with last selected list view.