0

I am new to visual force pages and have gone through some trailheads but am stuck on this part. Basically I want to submit an id from a contact record on the page and from that I want to query my salesforce cases for that contact and display them on the page. What I have included below works exactly the way I want except I have hardcoded the accountId. What I want to do is be able to get the value from the user in my controller, make a new SOQL statement to get the accountId and then execute my statement. What do I need to change on my visual force page to get input from the user and then display my info?

Below is my visual force page:

<apex:page controller="CaseListController">
<apex:form>
    <apex:pageBlock title="Case List" id="case_list">
        <apex:pageBlockTable value="{! cases }" var="ct">
        <apex:column value="{! ct.CaseNumber }"/>
        <apex:column value="{! ct.Owner.Name }"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>

Controller:

public class CaseListController {
public List<Case> getCases() {
String accountId = '0010400001234ABEEY';
List<Case> results = Database.query(
    'SELECT Id, CaseNumber, Owner.Name ' +
    'FROM Case ' +
    'Where AccountId = :accountId'
);
return results;
}

}
1
  • By the way, you only need Database.query(string) for dynamic queries, such as selecting variable columns. In your case, you can simply write Case[] results = [SELECT CaseNumber, Owner.Name FROM Case WHERE AccountId = :accountId]; It's considered a best practice to an inline query (as demonstrated here) whenever possible, as they are compile-time checked for syntax errors and are generally more capable.
    – sfdcfox
    Sep 16, 2022 at 16:32

1 Answer 1

2

You just need to read a parameter from the URL:

String accountId = ApexPages.currentPage().getParameters().get('id');

Which you then place into the URL:

/apex/myPage?id=0010400001234ABEEY

To make it dynamic, you can use URLFOR in different areas. For example, in a formula, you might use:

URLFOR($Page.myPage, null, [id=Account.Id])

Edit:

To allow a user to select an Id (or even better, a record), you can add a field. I'd start with something like this:

public class CaseListController {
  public Contact filterContact { get; set; }
  public Case[] caseRecords { get; set; }
  public CaseListController() {
    filterContact = new Contact();
    loadCaseList();
  }
  public void loadCaseList() {
    caseRecords = [SELECT CaseNumber, OwnerId FROM Case WHERE AccountId = :filterContact.AccountId];
  }
}

For which you'll want to use apex:inputField:

<apex:page controller="CaseListController">
    <apex:form>
        Search: <apex:inputField value="{!filterContact.AccountId}">
        <apex:actionSupport event="onchange" action="{!loadCaseList}" />
        </apex:inputField>
        <apex:pageBlock title="Case List" id="case_list">
            <apex:pageBlockTable value="{!caseRecords}" var="ct">
                <apex:column value="{! ct.CaseNumber }"/>
                <apex:column headerValue="Owner">
                    <apex:inputField value="{!ct.OwnerId}" />
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Note that we must use the Id field to save the value, so we use OwnerId instead of Owner.Name. The name will automatically be displayed.

8
  • will this allow the user to enter it on the page? Basically I just want a field on the visual force page where they can enter the id. Then the lookup will happen in the controller and post the result back to the page
    – MarkC5000
    Sep 16, 2022 at 17:41
  • 1
    @MarkC5000 No, it sounds like I misunderstood your question. Give me a minute.
    – sfdcfox
    Sep 16, 2022 at 17:44
  • thanks @sfdcfox
    – MarkC5000
    Sep 16, 2022 at 18:01
  • 1
    @MarkC5000 I meant to write CaseList there, actually. Edited.
    – sfdcfox
    Sep 16, 2022 at 20:35
  • 1
    @MarkC5000 I had renamed the variable, then forgot about it. 😅 The variable name in the markup needs to match the variable name in the controller. I actually built this code in my org to make sure I cleaned up all my mistakes. Give the new version a try.
    – sfdcfox
    Sep 19, 2022 at 14:21

You must log in to answer this question.

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