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
di_zoudi_zou 

How would I show ALL the cases of an account?

I am trying to make a custom page in my Customer Portal that shows ALL the cases of an account instead of just that specific user's. 

 

The SQL statement I am using in my VisualForce page to do this is:

 

    SELECT CaseNumber, AccountId, Subject FROM Case WHERE AccounttId = {!$User.AccountId}

 

However, I get this error:

 

    Error: Field AccountId does not exist. Check spelling 

 

How would I create this custom page that shows all cases?

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_
Id accountId = [SELECT Contact.AccountId FROM User WHERE Id = :UserInfo.getUserId()].Contact.AccountId;

//Don't forget to bring back the fields you need
List<Case> cases = [SELECT Id FROM Case WHERE AccountId = :accountId];

 

All Answers

Damien_Damien_

{!$User.AccountId} - I'm not quite sure where you got this from.... Users don't have an Account field.

 

You need to specify a real account id if you want to grab all cases associated with a specific Account.  If you want ALL accounts like your other question, you just remove the WHERE statement.

 

FYI... if you aren't using a controlller and have all your logic on your page, you aren't using Apex.  This is bad.  Look up how to use controllers instead of putting all your logic on your page.

di_zoudi_zou

I've been trying to use a controller like in my other post, but how would I let the controller know what Account I want based on the User?

Damien_Damien_

Your other post appears to NOT be using a custom controller.  I can tell by the syntax you're using {!User.AccountId}.  This is Visualforce specific.

 

but how would I let the controller know what Account I want based on the User?

This is a question I cannot answer for you.  You need to figure out what your requirements are.  Are you wanting to use the User's Contact's Account for example?  Or something else?

di_zoudi_zou

Ok, I will look into the documentation more about custom controllers. I would like to use the User's Contact's Account. This way, I cna have the controller get a list of all the cases associated with that account and return it to the Visualforce page to display.

Damien_Damien_
Id accountId = [SELECT Contact.AccountId FROM User WHERE Id = :UserInfo.getUserId()].Contact.AccountId;

//Don't forget to bring back the fields you need
List<Case> cases = [SELECT Id FROM Case WHERE AccountId = :accountId];

 

This was selected as the best answer
di_zoudi_zou

I got this code to work in my controller and to display the results on my Visualforce page. However, the cases that are showing up are only the cases for the specific user. None of the other cases for the user's account are showing up. I'd like tot see all the cases attached to that account as well.

Damien_2Damien_2

Somehow locked out of my other name...

 

I think the User doesn't have permissions to see those other Cases is my guess.  Code won't fix that unless you make your code unsecure by ignoring privileges.