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
levi6dblevi6db 

I need help adding check box fields

Hi all,

 

I created an object called Locations that pulls data from another custom object called FNL.  I created a button on the Opportunity object that will allow a rep to lookup a given FNL location and create a Location item.  I currently have the following features working: The FNL search fields, the matching values based on the search and the existing Locations that are already associated to the Opportunity.

 

What I am having trouble with is being able to create checkboxes next to the matching values that are returned that will allow me to add them to the Opportunity.

 

public class LocationSearchController {

 public PageReference Cancel() {
        return null;
    }

        public Opportunity o { get; set; }
    Id myid= ApexPages.currentPage().getParameters().get('id');
    
    public PageReference ViewData() {

    return null;
    }
     
  // the soql without the order and limit
  private String soql {get;set;}
  // the collection of FNL to display
  public List<FNL__c> fnl {get;set;}
 
  // the current sort direction. defaults to asc
  public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
    set;
  }
 
  // the current field to sort by. defaults to Name
  public String sortField {
    get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
    set;
  }
 
  // format the soql for display on the visualforce page
  public String debugSoql {
    get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
    set;
  }
 
  // init the controller and display some sample data when the page loads
  public LocationSearchController() {
    soql = 'select Name, FNL_Suite__c, FNL_City__c, FNL_State__c, FNL_Zip__c, Building_Code__c from FNL__c where Name != null';
    runQuery();
   
    o = [select Id, Name, OwnerId, Account.Name, RecordType.Name, Existing_Order_Number__c, AboveNet_Order_Number__c, Quote_MRC_Total__c, 
                 Quote_NRC_Total__c, TechChange__c, Commencement_Date__c, PrepaymentAmount__c, ImplementationComments__c, BillingComments__c 
                 from Opportunity 
                 where id = :myid];     
  }
  
  public Opportunity getOpportunity() {
            return o;
  }
 
 
    //Our collection of the class/wrapper objects cLoctact 
    public List<cLocation> locationList {get; set;}

    //This method uses a simple SOQL query to return a List of Contacts
    public List<cLocation> getLocations() {
        if(locationList == null) {
            locationList = new List<cLocation>();
            for(Location__c c : [select Id, Street__c, City__c, State__c, zip__c, Entrances_Required__c, Location__c, Proposed_Demarc__c,
                            Country__c, FNL_Building_Code__c, Location_Type__c, Available_Entrance__c, Building_Fiber_Demarc__c, Name, 
                            Building_is_OnNet__c, Building_Type__c, Suite__c, FNL_Street__c from Location__c where Account__c = :o.AccountId]) {
                // As each contact is processed we create a new cLoctact object and add it to the contactList
                locationList.add(new cLocation(c));
            }
        }
        return locationList;
    }
   
     public PageReference processSelected() {

        //We create a new list of Locations that we be populated only with FNL if they are selected
        List<Location__c> selectedLocations = new List<Location__c>();

        //We will cycle through our list of cLocations and will check to see if the selected property is set to true, if it is we add the FNL to the selectedLocations list
        for(cLocation cLoc : getLocations()) {
            if(cLoc.selected == true) {
                selectedLocations.add(cLoc.loc);
            }
        }

        // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
        List <Location__c> newloc = new List<Location__c>();

        for(Location__c loc : selectedLocations) {
            
            Location__c nloc; 
            nloc = loc.clone(false);
            nloc.Opportunity__c = o.Id;
            nloc.Account__c = null;
            
            newloc.add(nloc);
        }
        
        insert newloc;
        return null;
    }

//Our collection of the class/wrapper objects cLocations 
    public List<dfnl> fnlList {get; set;}

    //This method uses a simple SOQL query to return a List of FNL
    public List<dfnl> getfnl() {
        if(fnlList == null) {
            fnlList = new List<dfnl>();
               for(FNL__c d : Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20') ) 
            {
                // As each contact is processed we create a new cFNL object and add it to the Location List
                fnlList.add(new dfnl(d));
             }
        }
        return fnlList;
    }
   
     public PageReference processSelectedf() {

        //We create a new list of Contacts that we be populated only with Contacts if they are selected
        List<FNL__c> selectedfnl = new List<FNL__c>();

        //We will cycle through our list of cLoctacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedLocations list
        for(dfnl dF : getfnl()) {
            if(dF.selected == true) {
                selectedfnl.add(dF.fnl2);
            }
        }

        // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
        List <Location__c> newloc1 = new List<Location__c>();

        for(FNL__c fnl : selectedfnl) {
            
            Location__c nloc1;
            nloc1.Access_Type__c = fnl.Access_Type__c;
            nloc1.Assets__c = fnl.Assets__c;
            nloc1.FNL_Building_Code__c = fnl.Building_Code__c;
            nloc1.Building_Fiber_Demarc__c = fnl.Building_Fiber_Demarc__c;
            nloc1.Building_Type__c = fnl.Building_Type__c;
            nloc1.Datacenter__c = fnl.Datacenter__c;
            nloc1.Entrances_Required__c = fnl.Entrance__c;
            nloc1.Street__c = fnl.FNL_Street__c;
            nloc1.Suite__c = fnl.FNL_Suite__c;
            nloc1.City__c = fnl.FNL_City__c;
            nloc1.State__c = fnl.FNL_State__c;
            nloc1.Zip__c = fnl.FNL_Zip__c;
            nloc1.Country__c = fnl.FNL_Country__c;
            nloc1.IP_POP__c = fnl.IP_POP__c;
            nloc1.IP_VPOP__c = fnl.IP_VPOP__c;
            nloc1.LH_POP__c = fnl.LH_POP__c;
            nloc1.LH_VPOP__c = fnl.LH_VPOP__c;
            nloc1.Opportunity__c = o.Id;
            nloc1.Account__c = null;
            
            newloc1.add(nloc1);
        }
        
        insert newloc1;
        return null;
    }


    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class cLocation {
        public Location__c loc {get; set;}
        public Boolean selected {get; set;}

        //This is the contructor method. When we create a new cLoctact object we pass a Contact that is set to the con property. We also set the selected value to false
        public cLocation(Location__c c) {
            loc = c;
            selected = false;
         }
    }
     
      // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class dFNL {
        public FNL__c fnl2 {get; set;}
        public Boolean selected {get; set;}

     //This is the contructor method. When we create a new cLoctact object we pass a Contact that is set to the con property. We also set the selected value to false
        public dFNL(FNL__c d) {
            fnl2 = d;
            selected = false;
         }
    }
  
     
  // toggles the sorting of query from asc<-->desc
  public void toggleSort() {
    // simply toggle the direction
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
    // run the query again
    runQuery();
  }
 
  // runs the actual query
  public void runQuery() {
 
    try {
      fnl = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
    }
 
  }
 
  // runs the search with parameters passed via Javascript
  public PageReference runSearch() {
 
    String Name = Apexpages.currentPage().getParameters().get('Name');
    String fnlcity= Apexpages.currentPage().getParameters().get('fnlcity');
    String buildingcode= Apexpages.currentPage().getParameters().get('buildingcode');
 
    soql = 'select Name, FNL_Suite__c, FNL_City__c, FNL_State__c, FNL_Zip__c, Building_Code__c from FNL__c where Name != null';
    if (!Name.equals(''))
      soql += ' and Name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';
    if (!fnlcity.equals(''))
      soql += ' and FNL_City__c LIKE \''+String.escapeSingleQuotes(fnlcity)+'%\'';
    if (!buildingcode.equals(''))
      soql += ' and Building_Code__c LIKE \''+String.escapeSingleQuotes(buildingcode)+'%\''; 
 
    // run the query again
    runQuery();

    return null;
  }
 

}

 

<apex:page controller="LocationSearchController" sidebar="false">
 
   <apex:form >
      <apex:pageBlock >
         <apex:pageBlockButtons >
            <apex:commandButton value="Add Selected Location to Opportunity" action="{!processSelected}" rerender="table" onclick="window.top.close()" oncomplete="javascript&colon;closeRefresh('{!Opportunity.Id}');"/>
            <apex:commandButton value="Cancel" action="{!Cancel}" rerender="table" onclick="window.top.close();"/>
         </apex:pageBlockButtons>
         <!-- In our table we are displaying the cContact records -->
         <apex:pageBlockTable value="{!locations}" var="c" id="table">
            <apex:column >
            <!-- This is our selected Boolean property in our wrapper class -->
             <apex:inputCheckbox value="{!c.selected}"/>
            </apex:column>
                <!-- This is how we access the contact values within our cContact container/wrapper -->
                   <apex:column value="{!c.loc.Name}" />
                   <apex:column value="{!c.loc.Street__c}" />
                   <apex:column value="{!c.loc.City__c}" />
                   <apex:column value="{!c.loc.State__c}" />
         </apex:pageBlockTable>
      </apex:pageBlock>
   </apex:form>
    
   <apex:form >
      <apex:pageMessages id="errors" />
 
      <apex:pageBlock title="FNL Lookup" mode="edit">
         <table width="100%" border="0">
            <tr>  
               <td width="200" valign="top">
                  <apex:pageBlock title="Parameters" mode="edit" id="criteria">
                     <script type="text/javascript">
                        function doSearch() {
                        searchServer(
                        document.getElementById("Name").value,
                        document.getElementById("fnlcity").value,
                        document.getElementById("buildingcode").value);
                        } 
                     </script> 
 
                     <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
                        <apex:param name="Name" value="" />
                        <apex:param name="fnlcity" value="" />
                        <apex:param name="buildingcode" value="" />
                     </apex:actionFunction>
 
                     <table cellpadding="2" cellspacing="2">
                        <tr>
                           <td style="font-weight:bold;">Street<br/>
                              <input type="text" id="Name" onkeyup="doSearch();"/>
                           </td>
                        </tr>
                        <tr>
                           <td style="font-weight:bold;">City<br/>
                              <input type="text" id="fnlcity" onkeyup="doSearch();"/>
                           </td>
                        </tr>
                        <tr>
                           <td style="font-weight:bold;">Building Code<br/>
                              <input type="text" id="buildingcode" onkeyup="doSearch();"/>
                           </td>
                        </tr>
                     </table>
                  </apex:pageBlock>
               </td>
                 <td valign="top">
                    <apex:pageBlock mode="edit" id="results">
                    <!-- In our table we are displaying the cContact records -->
                       <apex:pageBlockTable value="{!fnl}" var="d" id="FNLtable">

                            <apex:column value="{!d.Name}" />
                            <apex:column value="{!d.FNL_Suite__c}" />
                            <apex:column value="{!d.FNL_City__c}" />
                            <apex:column value="{!d.FNL_State__c}" />
                            <apex:column value="{!d.FNL_Zip__c}" />
                            <apex:column value="{!d.Building_Code__c}" />
                       </apex:pageBlockTable>
                       <apex:pageBlockButtons >
                          <apex:commandButton value="Add Selected Location to Opportunity" action="{!processSelected}" rerender="table" onclick="window.top.close()" oncomplete="javascript&colon;closeRefresh('{!Opportunity.Id}');"/>
                       </apex:pageBlockButtons>
                    </apex:pageBlock>
                 </td>
            </tr>
         </table>
            <apex:pageBlock title="Debug - SOQL" id="debug">
               <apex:outputText value="{!debugSoql}" />           
            </apex:pageBlock>    
      </apex:pageBlock>
   </apex:form>
</apex:page>

 Any help would be appriciated.

 

Thanks.

ColinKenworthy2ColinKenworthy2

Which button performs "processSelectedf" ? or isn't that method needed?

levi6dblevi6db

That is the method needed, plus, how do I format the checkbox on the VF page?

 

Thanks.

ColinKenworthyColinKenworthy
But what runs that method, I see nothing at all that calls it.
levi6dblevi6db

I do not have anything added yet, that is the part where I am stuck.  I do not know how to process/add checkboxes.

 

Thank you for responding by the way.

ColinKenworthy2ColinKenworthy2

Surely it would be

 

<apex:column >
    <!-- This is our selected Boolean property in our wrapper class -->
    <apex:inputCheckbox value="{!d.selected}"/>
</apex:column>

 

levi6dblevi6db

I tried that before and received this error:

 

Error: Invalid field selected for SObject FNL__c

 

I put the column here:

 

<apex:pageBlockTable value="{!fnl}" var="d" id="FNLtable">
                            <apex:column >
                             <!-- This is our selected Boolean property in our wrapper class -->
                             <apex:inputCheckbox value="{!d.selected}"/>
                            </apex:column>
                            <apex:column value="{!d.Name}" />
                            <apex:column value="{!d.FNL_Suite__c}" />
                            <apex:column value="{!d.FNL_City__c}" />
                            <apex:column value="{!d.FNL_State__c}" />
                            <apex:column value="{!d.FNL_Zip__c}" />
                            <apex:column value="{!d.Building_Code__c}" />

 

Is that hte correct spot?  I still receive the error.

 


ColinKenworthy2ColinKenworthy2

The pageBlockTable uses controller property "fnl" so it will use a method "getfnl" in the controller.

 

In your controller you have variable fnl with a default getter method :

public List<FNL__c> fnl {get;set;}

 and a getfnl method :

    //This method uses a simple SOQL query to return a List of FNL
    public List<dfnl> getfnl() {
        if(fnlList == null) {
            fnlList = new List<dfnl>();
               for(FNL__c d : Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20') ) 
            {
                // As each contact is processed we create a new cFNL object and add it to the Location List
                fnlList.add(new dfnl(d));
             }
        }
        return fnlList;
    }

 Looks like you have 2 "getfnl" and it appears to be returning the first one, which is a List<FNL__c> and hence the error.

 

If you don't need that variable then delete it. Then you'll need to amend your page block table to use the List<dfnl> that you will now get back.

                      <apex:column >
 <!-- This is our selected Boolean property in our wrapper class -->
 <apex:inputCheckbox value="{!d.selected}"/>
 </apex:column>
 <apex:column value="{!d.fnl2.Name}" /> <apex:column value="{!d.fnl2.FNL_Suite__c}" /> etc. etc.