3

I have a VF page that uses a custom controller.

The page is a summary page with multiple objects on the page, each one has a check box, that when checked, and saved will not display the object anymore.

I have commandLink action to a method in my controller.

public PageReference doChecks() { return null; }

If I could get the names of the checked boxed in my apex controller, I could save the data.

Here's my VF page:

<apex:page showHeader="false" sidebar="false" title="Warrany Case Info" controller="WarCase_controller">
<apex:pageBlock title="Cases Summary" tabStyle="Account"> 
    <apex:form >
        <apex:commandLink action="{!doChecks}" value="Hide Checked items"/>
        <table style="border-bottom:1px solid black;paddinf-bottom:20px" width="100%"><tr>
            <td style="border-right:1px solid black;padding-right:20px" valign="top">
            <font size="3em">New Cases (Created in the last 7 days)</font><br/>
            <apex:dataTable value="{!newCases}" var="case" id="theTable1"  width="100%" >
                    <apex:column >
                        <input type="checkbox" name="CH-New-{!case.ID}"/>
                        <apex:inputField value="{!case.summaryNewHide__c}"/>
                        <apex:outputLink value="/{!case.ID}" target="_top" style="padding-right:5px">{!case.Account.Name} ({!case.Owner_Name__c})</apex:outputLink>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">Short Desc&nbsp;</apex:facet>
                        <apex:outputText value="{!case.Short_Desc__c}"/>
                    </apex:column>

                </apex:dataTable>
            </td>
            <td style="padding-left:20px" valign="top">
                <font size="3em">Closed Cases (Closed in the last 7 days)</font><br/>
                <apex:dataTable value="{!closedCases}" var="case" id="theTable2"  width="100%">
                        <apex:column >
                            <input type="checkbox" name="CH-Closed-{!case.ID}"/>
                            <apex:facet name="header">Account Name (Owner Name)&nbsp;</apex:facet>
                            <apex:outputLink value="/{!case.ID}" target="_top" style="padding-right:5px">{!case.Account.Name} ({!case.Owner_Name__c})</apex:outputLink>
                        </apex:column>

                        <apex:column >
                            <apex:facet name="header">Short Desc</apex:facet>
                            <apex:outputText value="{!case.Short_Desc__c}"/>
                        </apex:column>

                </apex:dataTable>

            </td>
        </tr></table>

        <br/>
        <font size="3em">Upcoming Scheduled Tasks (Scheduled for the next 7 days)</font><br/>

        <apex:dataTable value="{!tasks}" var="task" id="theTable"  width="100%" >

            <apex:column >
                <input type="checkbox" name="CH-Task-{!task.ID}"/>
                <apex:facet name="header">Account Name (Owner Name)&nbsp;</apex:facet>
                <apex:outputLink value="/{!task.ID}" target="_top" style="padding-right:5px">{!task.case__r.Account.Name} ({!task.case__r.Owner_Name__c})</apex:outputLink>
            </apex:column>

            <apex:column >
                <apex:facet name="header">Short Desc&nbsp;</apex:facet>
                <apex:outputText value="{!task.case__r.Short_Desc__c}" style="padding-right:5px"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">Task Description&nbsp;</apex:facet>
                <apex:outputText value="{!task.Description__c}" style="padding-right:5px"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">Scheduled Date</apex:facet>
                <apex:outputText value="{0,date,MM-dd-yyyy}">
                    <apex:param value="{!task.Schedule_Date__c}"/>
                </apex:outputText>
            </apex:column>

        </apex:dataTable>
    </apex:form>
</apex:pageBlock>

And my controller:

public with sharing class WarCase_controller {

public List<Case> getNewCases(){

    Date tempDate = Date.today(); 
    tempDate = tempDate.addDays(-7);

    List<Case> caseList=[
        Select Account.Name, Description, Owner_Name__c, Short_Desc__c, ID 
        From Case where ownerId=:Userinfo.getUserId() and TitanClosedDate__c=null and TitanCreatedDate__c>:tempDate and summaryNewHide__c=false ];

    return caseList;
}

public List<Case> getClosedCases(){

    Date tempDate = Date.today();
    tempDate = tempDate.addDays(-7);

    List<Case> caseList=[
        Select Account.Name, Description, Owner_Name__c, Short_Desc__c, ID 
        From Case where ownerId=:Userinfo.getUserId() and TitanClosedDate__c>:tempDate and summaryCloseHide__c=false];

    return caseList;
}

public List<Warranty_Task__c> getTasks(){ 
    Date tempDate = Date.today();
    tempDate = tempDate.addDays(7);

    List<Warranty_Task__c> caseList=[
        Select case__r.Account.Name, case__r.Owner_Name__c, case__r.Short_Desc__c, Description__c, Schedule_Date__c, ID 
        From Warranty_Task__c where case__r.ownerId=:Userinfo.getUserId() and Schedule_Date__c<:tempDate and summaryHide__c=false];

    return caseList;
}

public PageReference doChecks() {
    return null;

}

}

1
  • Are there a fixed number of check boxes that you want to get the values form? If so, you could bind the apex:inputCheckbox values to properties in the controller. Commented Apr 17, 2013 at 2:49

3 Answers 3

1

The answer I was looking for was:

Apexpages.currentPage().getParameters()

I can call this from my action method to get all the named form fields.

0

It would have been helpful it you'd posted some of your code. That said, I believe the gist of your question is: "Should I use apex:inputField or standard HTML to get the controller to save my data?"

If that's the essence of your question, the answer is there are several options available, but <apex:inputField> would definitely do the trick! Make certain you've set up the page with opening and closing <Apex:form> tags too. I'd highly recommend you download the VisualForce Workbook from the Salesforce Developer Resource pages. I think you'll find it a useful resource for help with questions of this sort. :)

4
  • My real question is: How do I get values from form fields when I submit the form to my controller? The method in the controller doesn't take any parameters, such as "HttpServletRequest" in java.
    – Lance
    Commented Apr 17, 2013 at 15:23
  • Lance, I think the best thing I can do for you is refer you to the Salesforce VisualForce Workbook for the kind of help you likely need. Its a downloadable PDF that you can use as a handy reference which also has tutorials on how to do these things.
    – crmprogdev
    Commented Apr 17, 2013 at 15:43
  • I looked at the workbook. It doesn't have anything like what I'm trying to do. 1) Most of tutorials in the workbooks use standard controllers. 2) Any page that has form fields is a detail page, where the entire page is for one object. I have a list of objects.
    – Lance
    Commented Apr 17, 2013 at 16:03
  • Do you have a list of "Object fields" or more than one list of "Objects with fields"? Please edit your original post with objects/fields and preferably code. An object would be like Account, Contact, Opportunity, etc. A field would be like Account.Name, Account.Id, Opportunity.Name, User.email, etc and either could include custom objects and/or fields that are appended with __c like: My_Custom_Object__c.My_Custom_Field__c.
    – crmprogdev
    Commented Apr 17, 2013 at 16:18
0

For your scenario, allow users to select an object, you want to use a wrapper class with a boolean for a checkbox. This is a pretty common design patter for Salesforce and allows you to tell what was selected by the user.

Wrapper Class

public class SelectableAccount {
  public Boolean selected { get; set; }
  public Account account { get; set; }

  public SelectableAccount(Account account) {
    this.account = account;
    selected = false;
  }
}

Controller

public class MyController {

  public List<SelectableAccount> selectableAccounts { get; set; }

  public MyController() {
    selectableAccounts = new List<SelectableAccount>();
    for(Account account : [select .... from Account where ...]) {
       selectableAccounts.add(new SelectableAccount(account));
    }
  }

  public void doChecked() {
    for(SelectableAccount sAccount : selectableAccounts) {
      if(sAccount.selected == true) {
        // your logic
      }
    }
  }
}

Visualforce Page

<apex:commandButton value="Submit" action="{!doChecked}"/>
<apex:pageBlockTable value="{!selectableAccounts}" var="sAccount">
  <apex:column headerValue="Select">
    <apex:inputCheckbox value="{!sAccount.selected}"/>
  </apex:column>
  <apex:column value="{!sAccount.account.name}"/>
</apex:pageBlockTable>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .