I have an APEX class with a controller extension. This extension pulls in child Service records on a related Opportunity. If you look at the getServices
list, you'll see it references opp.Id
. This works perfectly.
What I wanted to add was an error catch, because this is all tied to a VF related list. So if no records are pulled in from the query, then I would want it to render a different VF display instead of a blank related list.
To do this, I declared a recordCount
integer, and have it set to a database count query. It's essentially the same query as the one in the getQServices
query except it's selecting COUNT()
.
public class serviceViewController {
public final Opportunity opp;
Integer recordCount;
public List<QService__c> getQServices{get; set;}
public serviceViewController(ApexPages.StandardController controller) {
this.opp = (Opportunity)controller.getRecord();
//Get count of records pulling in.
recordCount = database.countQuery('SELECT COUNT(Id) FROM QService__c WHERE Opportunity__c = :opp.Id LIMIT 1');
system.debug('The record count is ' + recordCount);
//Pull in columns for QService__c and load into getQServices list
getQServices = [SELECT Id, Name, Total_Service_Cost__c
FROM QService__c
WHERE Opportunity__c = :opp.Id
];
}}
Where the VF page should just render the list, I get this error instead.
My VF code doesn't reference recordCount
because I haven't added the functionality where the controller extension is determining which page to render based off of the recordCount
(which means the page should render as normal) because I want to get the recordCount
pulled in correctly first.
Here's what I know for sure:
I know the query is correct because I've tested it by setting the opp.Id to a hardcoded opp id.
The developer console in SFDC allows this APEX class to save.
I tried making the recordCount
integer a List of object QService__c
instead. The error I got there was an illegal assignment from List<AggregateResult>
to List<QService__c>
, which makes sense.
I'm at a standstill on what the issue might be here. Am I going with best practice here on trying to pull in a count from a query and setting it to a variable or is there something better I should be doing?
opp
in your markup. Read up on property syntax and changepublic final Opportunity opp;
topublic Opportunity opp { get; private set; }
– Adrian Larson♦ Mar 12 '18 at 19:16WHERE Opportunity__c = :oppId LIMIT 1
– Daniel Ballinger Mar 12 '18 at 19:16