+ Start a Discussion
asoyerasoyer 

Passing selected record ids to Apex controller from List View

I'm looking for an alternative to passing selected records from a List View via the URL.  Here is how I'm currently doing it, but I'm running into IE URL limits when more than 100 records are selected.  This is a custom List view button executing JavaScript.

 

var ids = {!GETRECORDIDS( $ObjectType.Account )};

if(ids.length < 1 ) {
alert('Please select at least one Account.')
}
else {
// window.open('/apex/Quick_Email?rec='+ids.join(',')+'&retURL=001/o');
window.location='/apex/Quick_Email?rec='+ids.join(',')+'&retURL=001/o';
}

 

What other methods could I implement?

 

Best Answer chosen by Admin (Salesforce Developers) 
tedwardtedward

You could create a custom list view button that points to a visual force page that uses the standard list controller and uses a custom controller extension to do the actions you want.  For example:

 

VF Page:

 

<apex:page
  standardController="Account"
  recordSetVar="accounts"
  extensions="quickEmailController">
  <apex:form >
     <apex:pageBlock >
       <apex:pageBlockTable value="{!selected}" var="account">
         <apex:column value="{!account.name}"/>
       </apex:pageBlockTable>
       <apex:pageBlockButtons location="bottom">
         <apex:commandButton value="Do Something" action="{!doSomething}"/>
       </apex:pageBlockButtons>
     </apex:pageBlock>
  </apex:form>
</apex:page>

 

Extension controller:

 

public class quickEmailController{
    
    ApexPages.StandardSetController setCon;
    
    public quickEmailController(ApexPages.StandardSetController controller)
    {
        setCon = controller;
    }

    public pageReference doSomething()
    {
        // do something with the selected records
        for ( Account acc : (Account[])setCon.getSelected() )
        {
                System.debug('Account name = ' + acc.Name);
        }
        return null;
    }
}

 

All Answers

tedwardtedward

You could create a custom list view button that points to a visual force page that uses the standard list controller and uses a custom controller extension to do the actions you want.  For example:

 

VF Page:

 

<apex:page
  standardController="Account"
  recordSetVar="accounts"
  extensions="quickEmailController">
  <apex:form >
     <apex:pageBlock >
       <apex:pageBlockTable value="{!selected}" var="account">
         <apex:column value="{!account.name}"/>
       </apex:pageBlockTable>
       <apex:pageBlockButtons location="bottom">
         <apex:commandButton value="Do Something" action="{!doSomething}"/>
       </apex:pageBlockButtons>
     </apex:pageBlock>
  </apex:form>
</apex:page>

 

Extension controller:

 

public class quickEmailController{
    
    ApexPages.StandardSetController setCon;
    
    public quickEmailController(ApexPages.StandardSetController controller)
    {
        setCon = controller;
    }

    public pageReference doSomething()
    {
        // do something with the selected records
        for ( Account acc : (Account[])setCon.getSelected() )
        {
                System.debug('Account name = ' + acc.Name);
        }
        return null;
    }
}

 

This was selected as the best answer
asoyerasoyer

Thanks tedward.  this worked great.  The StandardSetController did the trick.

 

ImpactMGImpactMG

Is it possible to use this technique without a Custom Button?

 

I would like to call a visualforce page like the one shown above with a set of records from another Visualforce Page with own Apex Controller:

 

public with sharing class myController {
  list<Account> Accounts;

public PageReference CallOtherVisualforcePageWithListOfAccounts{ ApexPages.PageReference myRef = Page.MyPage; // Pass Accounts to myRef !?! } }

 

is there a way to accomplish that?

AthiAthi
Is it possible to use same techniques for all records in the list view.. Alternate to getSelected().
Padma NeelaPadma Neela
I tried this for custom object it is not working , what could be reason to get the issue ?