+ Start a Discussion
DManelskiDManelski 

List button on Opportunity related lists on Contact & Account

Hello,

I'm trying to write a very simple list button to update Opportunities from the Opportunity related lists on contact and account.  I'd like to be able to check off a few opportunity records and then click a button to change them as Closed Won or Closed Lost.  Ideally, I'd like the extension to just perform the action without having to confirm/save the records on a separate VF page.  In other words, all I need a VF page to do is execute the action and return the user to the contact or account record from which he/she started.  Here's what I've got so far:

Visualforce page:

Code:
<apex:page standardController="Opportunity" recordSetVar="opportunities" extensions="ONEN_EXT_UpdateOpportunities" action="{!MarkAsWon}">
</apex:page>

And the extension:
Code:
public class ONEN_EXT_UpdateOpportunities {

 public List<Opportunity> selectedOpps = new List<Opportunity>();

 public ONEN_EXT_UpdateOpportunities(ApexPages.StandardSetController controller) {
             this.selectedOpps = (list<Opportunity>)controller.getRecords();
        }
 
 public pageReference MarkAsWon() {
  
  List<Opportunity> OppsToUpdate = new List<Opportunity>();
  for (Opportunity newOpp : selectedOpps) {
   newOpp = new Opportunity (
    id = newOpp.id,
    StageName = 'Won Not Thanked'
   );
   OppsToUpdate.add(newOpp);
  }
  
  if (OppsToUpdate.size() > 0) {
   update OppsToUpdate;
  }
  
  PageReference p = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
p.setRedirect(true);
return p; } }

I just don't know how to retrieve/set/manipulate the selected records in my extension. Any guidance would be greatly appreciated.

Dave



Message Edited by DManelski on 12-19-2008 08:32 AM
Ron HessRon Hess
you can call getSelected() on the controller object.

so

controller.getSelected();
see :

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_standardsetcontroller.htm
DManelskiDManelski
That did the trick, thank you Ron!
IC-TannerIC-Tanner
Hi Ron, I have a a similar code snippet but I'm having trouble with the test class. The reason being that I don't know how to simulate "selected records" for the getSelected() method. I tried changing the 'getSelected() method to getRecords() which allowed test coverage and proved that the problem lies with the getSelected() method. Therefore I'm unable to process the 'for' statement because I'm unable to replicate getSelected for the "StandardSetController". So I am unable to test the following lines:

       if (participation.Status__c != null) {
       sp.Status__c = participation.Status__c; }
       if (participation.Total_Fees__c != null) {
       sp.Total_Fees__c = participation.Total_Fees__c; }
       if (participation.Status__c != null && participation.Total_Fees__c != null){
       newParticipants.add(sp); }
      }

--------------------
CLASS CODE
--------------------

public class MassEventParticipantUpdate {

participation__c participation;

public participation__c  getParticipation() {
      if(participation == null) participation = new participation__c();
      return participation;
   }

 public List<Participation__c> selectedParticipants = new List<Participation__c>();

 public MassEventParticipantUpdate(ApexPages.StandardSetController controller) {
             this.selectedParticipants = (list<Participation__c>)controller.getSelected();
        }
 
 public pageReference updateParticipants() {
 
  List<Participation__c> newParticipants = new List<Participation__c>();
  for (Participation__c sp:selectedParticipants) {
 
       if (participation.Status__c != null) {
       sp.Status__c = participation.Status__c; }
       if (participation.Total_Fees__c != null) {
       sp.Total_Fees__c = participation.Total_Fees__c; }
       if (participation.Status__c != null && participation.Total_Fees__c != null){
       newParticipants.add(sp); }
      }
 
   if (newParticipants.size() > 0) {
   update newParticipants;
  }
 
  PageReference p = new PageReference('/a0O/o');
  p.setRedirect(true);
  return p;
 }
 
  static testMethod void testMassEventParticipantUpdate() {
 
  List<Participation__c> testList = new list<participation__c>();
  participation__c p1 = new participation__c(Status__c='Invited');
  testList.add(p1);
  ApexPages.StandardSetController s = new ApexPages.standardSetController(testList);
  MassEventParticipantUpdate wizard = new MassEventParticipantUpdate(s);
  wizard.getParticipation();
  wizard.participation.Status__c='Invited';
  wizard.participation.Total_Fees__c=45;
  wizard.updateParticipants();
 }
}

Thanks ahead of time!


Message Edited by IC-Tanner on 01-20-2009 02:33 PM
DManelskiDManelski
You have to set the selected records in the standard set controller using the setSelected() method.

Your code should look like this:

Code:
static testMethod void testMassEventParticipantUpdate() {
  
  List<Participation__c> testList = new list<participation__c>();
  participation__c p1 = new participation__c(Status__c='Invited');
  testList.add(p1);
  ApexPages.StandardSetController s = new ApexPages.standardSetController(testList);

s.setSelected(testList);

MassEventParticipantUpdate wizard = new MassEventParticipantUpdate(s); wizard.getParticipation(); wizard.participation.Status__c='Invited'; wizard.participation.Total_Fees__c=45; wizard.updateParticipants(); }