You need to sign in to do that
Don't have an account?
Sylvie Serplet 1
SOQL for last Cases for Contact
Hi,
I am trying to write a query to get a list of the last 5 cases from the contact in the current case. A Visualforce page will display this list in the Case layout. That is what I have come with but I could not make the In clause to work. Any ideas?
Thank you for your help.
Sylvie
I am trying to write a query to get a list of the last 5 cases from the contact in the current case. A Visualforce page will display this list in the Case layout. That is what I have come with but I could not make the In clause to work. Any ideas?
Select Id, CaseNumber, CreatedDate, Status, Subject, ContactId FROM Case Where ContactId In:.............. Order by CaseNumber DESC Limit 5
Thank you for your help.
Sylvie
Otherwise, if you're querying for multiple contacts, the correct syntax is:
In the second example, contactIds is a collection (either set or list) of Contact Ids.
Unfortunately I got the message " Error: Compile Error: Variable does not exist: contactId at line 8 column 107"
This is the complete Apex Class
In the constructor, it pulls in the case, including it's contactid field from the Case standard controller (passed in automatically via Visualforce).
Next, it queries for the list of cases associated with that same contact id.
A huge THANK YOU for your help.
Sylvie
The example class that James provided is an Apex extension, so your page tag should include the standardController and extenions attribute. Without standardController="Case", you will not be able to include the Visualforce page in the Case layout. Hope this helps!
My Visualforce page is now:
And my Apex class:
This is the error message I get:
Error: Compile Error: member variable not visible for assignment at line 7 column 14
Thank you for your help.
Sylvie
[Error] Error: Compile Error: The method List<Case> getCA() is referenced by Visualforce Page (CaseList) in salesforce.com. Remove the usage and try again. at line 8 column 19
The table being read-only, I went ahead and gave the CA property a private setter. I also had to use addFields() in order to include the ContactId field with the Case when getting out of the standard controller.
I also found that the Case for which the page is loaded also appeard in the Case List table. So I have updated the where clause to exclude this case from the results. Does this get you what you need?
I have also added a link on the case number that open the case in a new tab as below:
I have also written the test but it does not pass. Could you please tell me what is wrong whit it? Thank you.
I have tried solutions 1 and 3 and the test still not passed.
For solutions No. 2 I am not sure where to insert the if statement. Do I need to delete some lines in my test above?
Sorry I am a beginner! Thank you for your time.
I believe you are encountering an error that says, "You cannot call addFields when the data is being passed into the controller by the caller."
1. You can add an if statement to the CaseListController constructor, checking to see whether a test is running. This way you are only calling the addFields() method when the unit test is not running (also meaning your unit test would never execute this line of actual code). 2. You can also REMOVE the addFields() call from your constructor and include a hidden reference to the field in your visualforce markup. Any field referenced in your page will automatically be included in the standard controller. Either of these approaches should get your unit test to pass. You would probably also be creating some additional cases in your test in order to assert that the list includes the expected results.
Please share your updated classes and visualforce if you are still encountering the error.