Activity Forums Salesforce® Discussions By Map count all contact related to Account and,store contacts into a list.Do't use trigger

  • Aman

    Member
    August 8, 2017 at 12:26 pm

    Hello Raghav,

    you can try this :

    public class CountContacts {

    public static void getAllContact(){
    Map<Id,Integer> AccountIdVsContact = new Map<Id,Integer>();
    Set<Id> accid =new Set<Id>();
    List<contact> cont =[select Lastname,accountId from contact Limit 100];
    system.debug('@@@@'+cont.size());
    for(contact con: cont){
    accid.add(con.accountid);
    }

    for(Id id :accid){
    AccountIdVsContact.put(id, [select Lastname,accountId from contact where accountid= :id].size());
    }
    system.debug('#####'+AccountIdVsContact.values());
    }
    }

  • Daljeet

    Member
    February 26, 2022 at 7:01 pm

    but it isn't good to use soql queries in loop,right?

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos