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
tzuvytzuvy 

How to Get Case's Account ID on Apex Controller

Hi,

 

I'm creating a VF page that should be displayed in the case object.

 

I would like to retrieve information about the account  in which that case resides and pass it on to the apex controller in that page.

 

For instance let's say my controller looks like this

 

public class MyController {

      private final Account account;

      public MyController() {
            account = [select id, name, site from Account where id =
                       :ApexPages.currentPage().getParameters().get('id')];
      }

      public Account getAccount() {
            return account;
      }

      public PageReference save() {
            update account;
            return null;
      }
}

Becaus of the page being diplayed in the case, I can't querry for the Account ID this way

:ApexPages.currentPage().getParameters().get('id')

Since the id is not present in the case's url.

 

How can I pass the account ID from the case to the apex controller?

 

Any help will be much appreciated.

 

Thanks

 

Tzuvy

 

Best Answer chosen by Admin (Salesforce Developers) 
David81David81

I'd probably do it like this:

 

Account account = [SELECT Account.Name,Account.Site FROM Case WHERE Id = :ApexPages.currentPage().getParameters().get('id')].Account;

All Answers

Devendra NataniDevendra Natani

Instead of quering on account object you will have to query on case object.

 

 

List<Case> lstCase = [select Accountid, Account.name, Account.site from Case where id=: Apexpages.currentPage().getParameters().get('id)];

 

Then iterate over the lstCase to retrieve the account information.

 

 

Thanks,

Devendra

Blog

 

swathiswathi

Hi,

 

First query on Account object and then query on case object in which compare the accountid of case object with account object id 

David81David81

I'd probably do it like this:

 

Account account = [SELECT Account.Name,Account.Site FROM Case WHERE Id = :ApexPages.currentPage().getParameters().get('id')].Account;
This was selected as the best answer
tzuvytzuvy

Thanks this is what I was looking for