function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
philanthropyphilanthropy 

trouble keeping track of checkboxes selected

So I've posted on this learning project a few times now and I'm stuck again. I have two ways to rent a movie, a movie specific button created dynamically in the table and also a checkbox is available for each movie with a rent all checked movies button at the bottom.

 

I'm having a hard time with my code to keep track of the checked movies. Please let me know if you have any suggestions on how I could improve this or what I could use in the PageReference AddSelected. Am I making it more complicated than it has to be? 

 

public class RentalController {

   public List<Movie__c> movieList {get; set;}  
  
   public Movie__c movieToRent {get;set;}     
  
   public string movieIndex {get;set;}
  
   public Rentals__c newRentalRecord {get;set;}
  
   public string movieChecked {get;set;}
    
   List<Movie__c> checkedMovieList = new List<Movie__c>();
  
   // Used onClick for each movie checkbox
   public pageReference AddSelected () {
      for (Movie__c tempMovie : checkedMovieList) {
          if (tempMovie.ID==movieChecked) {
            // WRONG SYNTAX BELOW? NEED TO REMOVE MOVIE IF ALREADY WAS SELECTED BECAUSE ITS NOW BEING DESELECTED  
            //  checkedMovieList.remove(tempMovie);
          }
      }
      checkedMovieList.add([SELECT ID, name, description__c, price__c, total_inventory__c FROM Movie__c WHERE ID=:movieChecked]);
      return null;
   }  
     
   public RentalController () {
       movieList = [SELECT ID, name, description__c, price__c, total_inventory__c FROM Movie__c LIMIT 20];
   }
  
   // Used when Rental all Checked Movies button is selected
   public PageReference rentAllChecked () {
     Rentals__c newRentalRecord = new Rentals__c();
     insert newRentalRecord;
             
     for (movie__c tempMovieToRent : checkedMovieList) {
         tempMovieToRent.total_inventory__c=tempMovieToRent.total_inventory__c-1;
         update tempMovieToRent;        
         // Create new Unit Rental Record and Insert
         Unit_Rental__c newUnitRentalRecord = new Unit_Rental__c();
         newUnitRentalRecord.Movie__c =  tempMovieToRent.ID;
         newUnitRentalRecord.Rental__c = newRentalRecord.ID;  
         insert newUnitRentalRecord;              
     }
     
     // Redirect to Success page passing rental record ID
     PageReference p = Page.RentalSuccess;
     p.getParameters().put('id', newRentalRecord.ID);
     p.setRedirect(true);
     return p;
   }
  
   // Used when movie specific rental button is selected
   public PageReference rent() {
       // Query matching movie ID and assign as selected movie object
       movieToRent=[SELECT ID, name, total_inventory__c, price__c FROM Movie__C WHERE ID=:movieIndex];
  
       // Decrement movie inventory by 1
       movieToRent.total_inventory__c=movieToRent.total_inventory__c-1;
       update movieToRent;
      
       System.debug(movieToRent.name); //SUCCESS
      
       // Create new Rental Record and Insert
       Rentals__c newRentalRecord = new Rentals__c();
       insert newRentalRecord;
      
       // Create new Unit Rental Record and Insert
       Unit_Rental__c newUnitRentalRecord = new Unit_Rental__c();
       newUnitRentalRecord.Movie__c = movieToRent.ID;
       newUnitRentalRecord.Rental__c = newRentalRecord.ID;
       insert newUnitRentalRecord;
      
       // convert price to a string, should create seperate method for this
       string price;
       price= movieToRent.price__c.format();
      
       // Redirect to Success page passing movie name
       PageReference p = Page.RentalSuccess;
       p.getParameters().put('id', movieToRent.name);
       p.getParameters().put('price', price);
       p.setRedirect(true);
       return p;
    }       
}

 

<apex:page controller="RentalController"   >
 <h1>Rent a Movie</h1><br/><br/>
 <h2>Please Pick movie(s) to rent:</h2>
 <apex:form >
 <apex:pageBlock >
     <apex:pageBlockTable value="{!movieList}" var="movie_item" id="rentalTable">
         <apex:column headerValue="Movie Title">
             <apex:inputCheckbox id="movieChecked">
                 <apex:actionSupport event="onclick" action="{!AddSelected}" rerender="rentAllChecked">
                     <apex:param name="movieChecked" value="{!movie_item.ID}" assignTo="{!movieChecked}" />
                 </apex:actionSupport>
                
             </apex:inputCheckbox>
             <apex:outputText value="{!movie_item.name}"/>
         </apex:column>
         <apex:column headerValue="Description">
             <apex:outputText value="{!movie_item.Description__c}"/>
         </apex:column>
         <apex:column headerValue="Price">
             <apex:outputText value="${!movie_item.Price__c}"/>
         </apex:column>
         <apex:column headerValue="# In Stock">
             <apex:outputText value="{!movie_item.Total_Inventory__c}"/>
         </apex:column>
         <apex:column headerValue="Rent It">
             <apex:commandButton value="Rent {!movie_item.name}" action="{!rent}" rerender="rentalTable">             
             <apex:param name="movieIndex" value="{!movie_item.ID}" assignTo="{!movieIndex}" />
             </apex:commandButton>
         </apex:column>
     </apex:pageBlockTable><br/>
     <apex:commandButton id="rentAllChecked" value="Rent all Checked Movies" action="{!rentAllChecked}" rerender="rentalTable">             
         <!--<apex:param name="movieIndex" value="{!movie_item.ID}" assignTo="{!movieIndex}" />-->
     </apex:commandButton>  
 </apex:pageBlock>
 </apex:form>
</apex:page>

 

sfdcbynitesfdcbynite

what is the problem you are getting with the checkedMovieList.remove(tempMovie); ?

 

philanthropyphilanthropy

Error: RentalController Compile Error: Method does not exist or incorrect signature: [LIST<Movie__c>].remove(SOBJECT:Movie__c) at line 20 column 14

sfdcbynitesfdcbynite

List.remove uses takes an Integer which is the index of the element you want to remove. There is no method which will remove based on the actual object. So you need to iterate over the list and find the index of the obj to remove (or keep track of the indicies) or instead of a list you could use a map with the Id of Movie__c as the key.

philanthropyphilanthropy

Thanks for the suggestion! I guess I was unclear on the syntax of how list remove works. I ended up rewriting it using a wrapper class and instead of building my list onClick I just iterate through on the button click and identify which are selected.