Skip to content

Instantly share code, notes, and snippets.

@TheShubhamVsnv
Created June 30, 2021 12:55
Show Gist options
  • Save TheShubhamVsnv/3da3ee3efeaf105cd42adc9cde0d115e to your computer and use it in GitHub Desktop.
Save TheShubhamVsnv/3da3ee3efeaf105cd42adc9cde0d115e to your computer and use it in GitHub Desktop.
If 70% or more of the Contacts on an Account are Dead, mark the need_intel field to TRUE
public static void DeadIntel(list<Contact> cont){
Set<Id> accIds = new Set<Id>();
for(Contact conList : cont){
if(conList.accountId!=null){
accIds.add(conList.accountId);
}
}
Map<Id, List<Contact>> accContactMap = new Map<Id, List<Contact>>();
List<Account> accUpdateList = new List<Account>();
for(Contact obj : [SELECT accountId,Dead__c
FROM Contact
WHERE accountId IN :accIds]){
List<Contact> contLists;
if(accContactMap.containsKey(obj.accountId)){
contLists = accContactMap.get(obj.accountId);
}else{
contLists = new List<Contact>();
}
contLists.add(obj);
accContactMap.put(obj.accountId, contLists);
}
for(Id accId : accContactMap.keySet()){
Integer count_of_Dead = 0;
Integer total_con = accContactMap.get(accId).size();
if(accContactMap.get(accId).size() > 1){
for(integer i =0 ; i<accContactMap.get(accId).size(); i++)
if(accContactMap.get(accId)[i].Dead__c == true){
count_of_Dead++;
}
}
if((count_of_Dead*100)/total_con) > 70)
accUpdateList.add(new Account(id = accId, needintel__c = true));
}
if(!accUpdateList.isEmpty()){
update accUpdateList;
}
}
//Trigger
trigger UpdateContact on Contact (after update) {
if(Trigger.isUpdate && Trigger.isAfter){
AccountContact.DeadIntel(Trigger.new);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment