2

I want to create a multiple select list, in this if user select a single/multiple records from it and refreshes or selects next/previous page of a list then selected records from list should remain selected.

For example, I have created a button My Opportunities on account, After clicking on that button it redirects me to the VF page consisting list of all my opportunities(This much i have created with pagination and sorting). What I want is, When I select multiple opportunities from list and click on save button, the selected list should remain same even if i visit that page again in future. And there is one field created on account which adds the amount of selected opportunity to that field(I have covered this). What i want is when i deselect the selected records from list then the opportunity amount should get minus from the field that i have created on account.

2

If you want those selections to persist, then you need to store them somewhere. I would suggest storing them in a hidden field on the Account. You could for example, store a comma separated list of Opportunity Id's in a text field on the account.

Then each time your VF page is opened from an account, you load your list of Opportunities, and then you can use that comma separated list to check the opportunities that should be checked. Each time you save, you both update the comma separated list of Opportunity Ids, as well as the field that adds the Opportunity amount of all those fields.

Lets say you have the Opp IDs stored on the Account record. Your field would be

006XXXXXXXXXXX1,006XXXXXXXXXXX2,006XXXXXXXXXXX3,006XXXXXXXXXXX4

You would just query all your opps for that account

list<Opportunity> myOpps = [Select Id, Name, Amount From Opportunity Where AccountId =: myAccount.Id];

Then, you would query only the opps in your list, this would tell you which Opps to select.

list<Id> oppIDs = myAccount.Selected_Opps__c.split(',');
list<Opportunity> selectedOpps = [Select Id, Name From Opportunity Where Id IN : oppIDs];

You just then need to make sure you overwrite the Account.Selected_Opps__c field with the Ids of the opps that are actually selected, each time you save on your VF page.

  • Thank you for rapid response on my query. As I have performed functionality of pagination and sorting. If I click on next button and again come back to the same list where selection have made, My question is that will it work for pagination/sorting too ? – Vrushabh Jain Sep 25 '14 at 13:49
  • 1
    Depending on how you did your pagination, you may need to check the Opp IDs to see if any of your opps on the new page are in your list, and then check them. This would likely be done in your next method. – Chris Duncombe Sep 25 '14 at 13:51
  • Nope . there is no repeated Opps in list. Thanks I'll try this. – Vrushabh Jain Sep 25 '14 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.