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
Mohd NabeelMohd Nabeel 

If 70% or more of the Contacts on an Account are Dead, mark the need_intel field to TRUE.. What i am doing wrong in this question..

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/total_con)*100 > 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);
    }
}

 
Best Answer chosen by Mohd Nabeel
Colton WehkingColton Wehking
Try changing count_of_Dead and total_con to Decimals or change the line
if((count_of_Dead/total_con)*100 > 70)
to
if((count_of_Dead*100)/total_con) > 70)


 

All Answers

Colton WehkingColton Wehking
Try changing count_of_Dead and total_con to Decimals or change the line
if((count_of_Dead/total_con)*100 > 70)
to
if((count_of_Dead*100)/total_con) > 70)


 
This was selected as the best answer
Mohd NabeelMohd Nabeel
ohhh my bad.. Thanks
Ajay K DubediAjay K Dubedi
Hi Nabeel,

Try below code it works fine in my ORG
Your code's logic is correct it just needs Curly braces('{}') within 'for' loop

Trigger--->
trigger UpdateContact on Contact (after update)
{
    if(Trigger.isUpdate && Trigger.isAfter)
    {
        AccountContact.DeadIntel(Trigger.new);
    }
}

Class--->
public class AccountContact
{
    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++)                          //Here needed curly Braces
                {
                    if(accContactMap.get(accId)[i].Dead__c == true)
                    {
                        count_of_Dead++;
                    }
                }
            }
            if((count_of_Dead/total_con)*100 > 70)
            {
                accUpdateList.add(new Account(id = accId, needintel__c = true));   
            }
        }   
        if(!accUpdateList.isEmpty())
        {
            update accUpdateList;
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Deepali KulshresthaDeepali Kulshrestha
Hi Mohd,

I've gone through your code and you can replace your code from below code:
Apex Handler Class-->

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 con : [SELECT accountId,Dead__c FROM Contact WHERE accountId IN :accIds])
       {
                                
                    if( accContactMap.containsKey(con.AccountId)
                    {
                    accContactMap.get(con.AccountId).add(con);
                    }
                    else
                    {
                    accContactMap.put(con.AccountId,new List<Contact>());
                    accContactMap.get(con.AccountId).add(con);
                    }
                    
         }
         
        for(Id accId : accContactMap.keySet())
        {
            Integer count_of_Dead = 0;
            Integer total_con = accContactMap.get(accId).size();
            
            if(total_con > 1)
            {             
                for(integer i =0 ; i<total_con; i++)
                {
                    if(accContactMap.get(accId)[i].Dead__c == true)
                    {
                        count_of_Dead++;
                    }
            }
            if(((count_of_Dead/total_con)*100) > 70)
                accUpdateList.add(new Account(id = accId, needintel__c = true));  
        }  
        if(!accUpdateList.isEmpty()){
            update accUpdateList;
        }
    }


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Lola BenaLola Bena
Pls can someone give the pseudocode for the question asked above?