function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI 

Create a field on Account called “need_intel”, checkbox, default off Create a field on Contact called “Dead”, checkbox, default off Assignment: If 70% or more of the Contacts on an Account are Dead, mark the need_intel field to TRUE

Kishan Jangir 9Kishan Jangir 9
HI Syyash,

Please work this code:-

public class UpdateAccountHelper {
    
    public static void upAcc(Map<Id,Contact> mapCon) {
        
        Set<Id> accIds = new Set<Id>();
        List<Account> accListUpdate = new List<Account>();
        
        
        for(Contact con : mapCon.values()) {
            accIds.add(con.AccountId);
        }
        List<Contact> conList = [select accountid,dead__c from contact where accountid in :accIds ];
            system.debug('>>>>>>>>conList'+conList.size());
        
        for(Account acc : [select id,need_intel__c,(select accountid,dead__c from contacts where dead__c=true) from account where id in : accIds ]) {
            List<Contact> conList1 = acc.contacts;
            system.debug('>>>>>>>>conList1'+conList1.size());
            
            
            Integer per;
            if(conList1.size()>0 && conList.size()>0) {
             per = conList1.size()*100/conList.size();
            system.debug('>>>>>>per'+per);
            
            }
              
            
            if(per>=70) {
                acc.need_intel__c = true;
                
            }else {
                acc.need_intel__c = false;
            }
            accListUpdate.add(acc);
        }
        update accListUpdate;
        
    }

}


trigger UpdateAccountTrigger on Contact (after insert) {
    
    if(Trigger.isAfter==True && Trigger.isInsert==True) {
        UpdateAccountHelper.upAcc(Trigger.Newmap);
    }

}
S BishalS Bishal
Hi Kishan,

I have checked the above code you mentioned. It's working fine for a single contact insertion at a single time. But I fear that I will be working fine in case of insertion of a set of contact records at the same time. 

List<Contact> conList = [select accountid,dead__c from contact where accountid in :accIds ];
            system.debug('>>>>>>>>conList'+conList.size());

I think above SOQL query counting all the contacts irrespective of their Account Ids. 

So, I think It needs to be modified. I am also trying to solve it. I will update once i get the proper solution.

Thanks & Regards,
S Bishal
S BishalS Bishal
Hi SUYASH, 

you can check the below solution.
 
trigger UpdateAccforNeedIntel on Contact (after insert, After update) {
    
        Set<String> AccountIds = new Set<String>();
         for(Contact c : trigger.new){
        	AccountIds.add(c.AccountId);
    	}
        
		List<Account> ac = new List<Account>();
		Map<String,Integer> AccountwithDeadContacts = new Map<String,Integer>();
		Map<String,Integer> AccountwithoutDeadContacts = new Map<String,Integer>();
       
      	for(Account acc2 : [Select Id,Need_Intel__c,(Select Id,Dead__c From Contacts)
                            From Account Where Id in :AccountIds ]){
			List<Contact> con2=acc2.contacts;
            AccountwithoutDeadContacts.put(acc2.Id,con2.size());
        }
        
		for(Account acc1 : [Select Id,Need_Intel__c,(Select Id,Dead__c From Contacts Where Dead__c=true) 
                            From Account Where Id in :AccountIds ]){
			List<Contact> con1= acc1.contacts;
			AccountwithDeadContacts.put(acc1.Id,con1.size());
           Integer pert = (AccountwithDeadContacts.get(acc1.Id)*100/AccountwithoutDeadContacts.get(acc1.Id));
			if(pert>70){
				acc1.Need_Intel__c = true;
				ac.add(acc1);
			}
            else{
                acc1.Need_Intel__c = false;
                ac.add(acc1);
            }
		}
		update ac;
    
}

Hope this will help.

Thanks & Regards,
S Bishal