Skip to content

Instantly share code, notes, and snippets.

@mattparlane
Created July 14, 2017 13:55
Show Gist options
  • Save mattparlane/ac49a0d264f083d713fe6b1277d3c5d4 to your computer and use it in GitHub Desktop.
Save mattparlane/ac49a0d264f083d713fe6b1277d3c5d4 to your computer and use it in GitHub Desktop.
Add affiliations for non-Household accounts
Contact[] contacts = [select Id, AccountId from Contact where AccountId != null and Account.RecordType.DeveloperName != 'HH_Account'];
List<Id> contactIds = new List<Id>();
for (Contact c : contacts) contactIds.add(c.Id);
npe5__Affiliation__c[] affs = [select npe5__Contact__c, npe5__Organization__c from npe5__Affiliation__c where npe5__Contact__c in :contactIds];
npe5__Affiliation__c[] inserts = new List<npe5__Affiliation__c>();
for (Contact c : contacts) {
Boolean hasAff = false;
for (npe5__Affiliation__c aff : affs) {
if (aff.npe5__Contact__c != c.Id) continue;
hasAff = true;
break;
}
if (!hasAff) {
inserts.add(new npe5__Affiliation__c(
npe5__Contact__c = c.Id,
npe5__Organization__c = c.AccountId
));
}
}
insert inserts;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment