Skip to content

Commit

Permalink
21367: More Sfdc query updates
Browse files Browse the repository at this point in the history
  • Loading branch information
VSydor committed Sep 3, 2024
1 parent 36ff8f1 commit b775d80
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/main/java/com/impactupgrade/nucleus/client/SfdcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -666,23 +666,31 @@ public List<QueryResult> getDonatingContacts(Calendar updatedSince)
}

protected QueryResult queryDonatingContacts(String updatedSinceClause) throws ConnectionException, InterruptedException {
String opportunitySubQuery = """
SELECT ContactId, SUM(Amount) TotalAmount
FROM Opportunity
GROUP BY ContactId
HAVING SUM(Amount) > 0""";
List<SObject> aggregateResults = queryListAutoPaged(opportunitySubQuery);
String updatedContactsQuery = "select Id from Contact " +
"where " + updatedSinceClause;
List<SObject> updatedContacts = queryListAutoPaged(updatedContactsQuery);
if (CollectionUtils.isEmpty(updatedContacts)) {
return new QueryResult();
}
String updatedContactIds = updatedContacts.stream()
.map(sObject -> "'" + sObject.getField("Id") + "'")
.collect(Collectors.joining(","));

String totalOpportunityAmountQuery = "select ContactId, SUM(Amount) TotalAmount from Opportunity " +
"where ContactId in (" + updatedContactIds + ")" +
"group by ContactId " +
"having sum(Amount) > 0";
List<SObject> aggregateResults = queryListAutoPaged(totalOpportunityAmountQuery);
if (aggregateResults.isEmpty()) {
return new QueryResult();
}

String contactIds = aggregateResults.stream()
.map(sObject -> "'" + sObject.getField("ContactId") + "'")
.collect(Collectors.joining(","));

String query = "select " + getFieldsList(CONTACT_FIELDS, env.getConfig().salesforce.customQueryFields.contact, null) +
" from Contact " +
"where Id IN (" + contactIds + ")" + " AND " + updatedSinceClause;
"where Id in (" + contactIds + ")";
return query(query);
}

Expand Down

0 comments on commit b775d80

Please sign in to comment.