I have an Apex Class
that retrieves an aggregate result of Contacts
per Accounts
in order to validate a maximum of 2 Contacts
of a certain type.
I have tried several things. First I used a set and passed in all Account
ids, but I got no results. Then I built the query in a string and also got no results. But if I run the query in the developer console query editor I do get the results I expect. Why does my query not work in Apex
?
Apex:
public static map<id,String> beforeInsertHandler (Contact[] consList) {
//create a map of errors per contact and return at and of method if any entry added
map<id,String> exceptionsMap = new map<id,String>();
//get map of all principals per account being inserted
map<id,Contact> ContactsMap = new map<id,Contact>();
set <String> accIds = new set<String>();
for(Contact con:consList){
if(con.loan__Contact_Type__c == 'Principal' && con.Active__c==true){
ContactsMap.put(con.AccountId,con);
system.debug('### adding contact accIds: ' + accIds);
accIds.add(con.AccountId);
}
}
String query = 'Select count(id) cnt, AccountId From Contact where AccountId IN (\'';
for(String accid: accIds){
query+= accId + '\',';
}
String aggQuery = query.left(query.length()-1);
aggQuery += ') AND Active__c=true AND loan__contact_Type__c= \'Principal\' group by AccountId';
AggregateResult[] ars= database.query(aggQuery);
//THE QUERY ABOVE RETURNS 0 RECORDS WHEN I HAVE INSTERTED SEVERAL CONTACTS THAT MEET THE CRITERIA
//get aggregate count of principals per account and validate if active principals count is greater than 2
for(AggregateResult ar:ars) {
system.debug('### aggregate contacts:' + ar);
integer principalCnt = (integer)ar.get('cnt');
string accId = (string)ar.get('AccountId');
if(principalCnt >=2 && ContactsMap.containsKey(accId)) {
for(Contact con: consList) {
if(accId == con.AccountId) {
exceptionsMap.put(con.id,'The Account for this Contact already contains the maximum allowed of 2 active Principals.');
}
}
}
}
}
return
statement and hence won't compile.SOQL
verbatim.List<Contact>
?