Skip to content

Commit

Permalink
21367: More Sfdc query updates (accounts contacts added)
Browse files Browse the repository at this point in the history
  • Loading branch information
VSydor committed Sep 3, 2024
1 parent b775d80 commit 30a8db5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
56 changes: 47 additions & 9 deletions src/main/java/com/impactupgrade/nucleus/client/SfdcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ public List<QueryResult> getDonatingContacts(Calendar updatedSince)
updatedSinceClause = "SystemModStamp >= " + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(updatedSince.getTime());
}
queryResults.add(queryDonatingContacts(updatedSinceClause));
queryResults.add(queryDonatingAccountsContacts(updatedSinceClause));

if (updatedSince != null) {
updatedSinceClause = "Id IN (SELECT ContactId FROM CampaignMember WHERE SystemModStamp >= " + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(updatedSince.getTime()) + ")";
Expand All @@ -666,8 +667,12 @@ public List<QueryResult> getDonatingContacts(Calendar updatedSince)
}

protected QueryResult queryDonatingContacts(String updatedSinceClause) throws ConnectionException, InterruptedException {
String updatedContactsQuery = "select Id from Contact " +
"where " + updatedSinceClause;
if (Strings.isNullOrEmpty(updatedSinceClause)) {
env.logJobWarn("no filter provided; out of caution, skipping the query to protect API limits");
return new QueryResult();
}
String updatedContactsQuery = "SELECT Id FROM Contact " +
"WHERE " + updatedSinceClause;
List<SObject> updatedContacts = queryListAutoPaged(updatedContactsQuery);
if (CollectionUtils.isEmpty(updatedContacts)) {
return new QueryResult();
Expand All @@ -676,10 +681,10 @@ protected QueryResult queryDonatingContacts(String updatedSinceClause) throws Co
.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";
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();
Expand All @@ -688,9 +693,42 @@ protected QueryResult queryDonatingContacts(String updatedSinceClause) throws Co
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 + ")";
String query = "SELECT " + getFieldsList(CONTACT_FIELDS, env.getConfig().salesforce.customQueryFields.contact, null) + " " +
"FROM Contact " +
"WHERE Id IN (" + contactIds + ")";
return query(query);
}

protected QueryResult queryDonatingAccountsContacts(String updatedSinceClause) throws ConnectionException, InterruptedException {
if (Strings.isNullOrEmpty(updatedSinceClause)) {
env.logJobWarn("no filter provided; out of caution, skipping the query to protect API limits");
return new QueryResult();
}
String updatedAccountsQuery = "SELECT Id FROM Account " +
"WHERE " + updatedSinceClause;
List<SObject> updatedAccounts = queryListAutoPaged(updatedAccountsQuery);
if (CollectionUtils.isEmpty(updatedAccounts)) {
return new QueryResult();
}
String updatedAccountIds = updatedAccounts.stream()
.map(sObject -> "'" + sObject.getField("Id") + "'")
.collect(Collectors.joining(","));

String totalOpportunityAmountQuery = "SELECT AccountId, SUM(Amount) TotalAmount FROM Opportunity " +
"WHERE AccountId IN (" + updatedAccountIds + ")" +
"GROUP BY AccountId " +
"HAVING SUM(Amount) > 0";
List<SObject> aggregateResults = queryListAutoPaged(totalOpportunityAmountQuery);
if (aggregateResults.isEmpty()) {
return new QueryResult();
}

String accountIds = aggregateResults.stream()
.map(sObject -> "'" + sObject.getField("AccountId") + "'")
.collect(Collectors.joining(","));
String query = "SELECT " + getFieldsList(CONTACT_FIELDS, env.getConfig().salesforce.customQueryFields.contact, null) + " " +
"FROM Contact " +
"WHERE AccountId IN (" + accountIds + ")";
return query(query);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ default void batchFlush() throws Exception {
PagedResults<CrmAccount> getEmailAccounts(Calendar updatedSince, EnvironmentConfig.CommunicationList communicationList) throws Exception;
PagedResults<CrmContact> getSmsContacts(Calendar updatedSince, EnvironmentConfig.CommunicationList communicationList) throws Exception;
PagedResults<CrmContact> getDonatingContacts(Calendar updatedSince) throws Exception;
//TODO: getDonatingAccounts

// Map<Contact Id, List<Campaign Name>>
// We pass the whole list of contacts that we're about to sync to this all at once, then let the implementations
Expand Down

0 comments on commit 30a8db5

Please sign in to comment.