I am trying to count the number of account names that occur in my soql statement. Ultimately I have a map that shows all of the accounts near a technician and I want to show the number (count) of accounts on the map. When I try to create an aggregate expression on the SOQL string in the remote action portion of the apex class, I keep getting the error "Grouped Query Cannot Use Child Relationship in SELECT List." If I can't use the Count function in the SOQL String, is there another way I can do this?
public with sharing class NearbyTechnicianController {
public Account account { get; set; }
public SVMXC__Service_Group_Members__c sgm {get;set;}
public SVMXC__Installed_Product__c insp {get;set;}
public Integer NumAccts {get;set;}
// constructor
public NearbyTechnicianController() {
sgm = [select id, name, SVMXC__Street__c, SVMXC__City__c, SVMXC__State__c, SVMXC__Zip__c, Geolocation__Latitude__s, Geolocation__Longitude__s from SVMXC__Service_Group_Members__c where id = :ApexPages.currentPage().getParameters().get('id')];
account = [select Id, Name, CustomerMasterId__c, ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode, Geolocation__Latitude__s, Geolocation__Longitude__s, (Select SVMXC__Status__c, Name, Primary_FSE__c, SVMXC__Product__c, Model_Code__c from R00N70000001hzZ0EAI__r)
from Account limit 1];
insp = [select Id, Name, Model_Code__c from SVMXC__Installed_Product__c limit 1];
NumAccts = getNearbyTech.Size();
}
@RemoteAction
public static List<Account> getNearbyTech(Decimal latitude, Decimal longitude, String TechName) {
String fseLike = TechName + '%';
String q = 'select Id, Name, CustomerMasterID__c, ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode, Geolocation__Latitude__s, Geolocation__Longitude__s, (Select SVMXC__Status__c, Name, Primary_FSE__c, SVMXC__Product__c, Model_Code__c from R00N70000001hzZ0EAI__r) from Account ';
q += 'where DISTANCE(Geolocation__c, GEOLOCATION( ';
q += String.valueOf(latitude) + ', ' + String.valueOf(longitude);
q += ' ), \'km\') < 200';
q += 'AND id in (select SVMXC__Company__c from SVMXC__Installed_Product__c ';
q += 'where (Primary_FSE__c like :fseLike) AND (SVMXC__Status__c like \'Installed%\') AND ((Model_Code__c like \'DSD%\') OR (Model_Code__c like \'ADV%\') OR (Model_Code__c like \'EDG%\')))';
return Database.query(q);
}
}