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
Abhi92Abhi92 

Count Child Records By multiselect picklist and Place in separate fields (Trigger)

I have a use case, where I need to write a trigger on the Contact object to roll up the count to the Account based on the language selected in the Multiselect picklist field. 3 custom fields on Account: Member JP, Member FR, Member SP 1 custom field on Contact(multi-select) : Contact Type : Japanese, French, Spanish
Example: If in 1 contact user select in Contact Type: Japanese, French. Other Contact users select in Contact Type: Spanish, French, related to the same account. Member of JP should become - 1, Member FR - 2, Member SP - 1
I'm able to achieve this for single picklist value, but getting blocked on multiselect picklist use case
Can anyone please help me out in these!!
 
trigger ContactMultiselect on Contact (After insert, After Update, After delete, After Undelete) {
    Set<Id> AccID = new Set<Id>();
    List<Contact> conCanadianList = new List<Contact>();
    List<Contact> conFrenchList = new List<Contact>();
    List<Contact> conJapaneseList = new List<Contact>();
    List<Account> accountListToUpdate = new List<Account>();
    List<String> conType = new List<String>();
    IF(Trigger.IsAfter){
    IF(Trigger.IsInsert || Trigger.IsUndelete){
        for(Contact c : Trigger.new){
            if(c.AccountId!=null && String.isNotBlank(c.Contact_Type__c)){
                AccID.add(c.AccountId);
                }
            }
        List<Account> acctList = [Select id,Member_CA__c,Member_FR__c,Member_JP__c,(Select id,Contact_Type__c from Contacts) from Account Where Id in :AccID];
        List<Contact> conList = [Select id,Contact_Type__c from Contact Where AccountId in : AccID];
        for(Contact con : conList){
            If(con.Contact_Type__c == 'Canadian'){
                conCanadianList.add(con);
            }
            If(con.Contact_Type__c == 'French'){
                conFrenchList.add(con);
            }
            If(con.Contact_Type__c == 'Japanese'){
                conJapaneseList.add(con);
            }
        }
        update conCanadianList;
        update conFrenchList;
        update conJapaneseList;
        for(Account a : acctList){
            a.Member_CA__c = conCanadianList.size();
            a.Member_FR__c = conFrenchList.size();
            a.Member_JP__c = conJapaneseList.size();
        }
        try{
            update acctList;
        }
        Catch(DMLException e){
            for (contact con : Trigger.new){
                con.addError(e.getDMLMessage(0));
            }
        }
         }
    }
    IF(Trigger.isDelete){
        for(Contact con : Trigger.old){
            if(con.AccountId!=null && String.isNotBlank(con.Contact_Type__c)){
                AccID.add(con.AccountId);
            }
        }
        List<Account> acctList = [Select id,Member_CA__c,Member_FR__c,Member_JP__c,(Select id,Contact_Type__c from Contacts) from Account Where Id in :AccID];
        List<Contact> conList = [Select id,Contact_Type__c from Contact Where AccountId in : AccID];
        for(Contact con : conList){
            If(con.Contact_Type__c == 'Canadian'){
                conCanadianList.add(con);
            }
            If(con.Contact_Type__c == 'French'){
                conFrenchList.add(con);
            }
            If(con.Contact_Type__c == 'Japanese'){
                conJapaneseList.add(con);
            }
        }
        update conCanadianList;
        update conFrenchList;
        update conJapaneseList;
        for(Account a : acctList){
            a.Member_CA__c = conCanadianList.size();
            a.Member_FR__c = conFrenchList.size();
            a.Member_JP__c = conJapaneseList.size();
        }
        try{
            update acctList;
        }
        Catch(DMLException e){
            for (contact con : Trigger.new){
                con.addError(e.getDMLMessage(0));
            }
        }
    }
    IF(Trigger.IsUpdate){
        Set<Id> oldAccId = new Set<Id>();
        for(Contact c : Trigger.new){
            // for new contacts
            if(c.AccountId!=null && String.isNotBlank(c.Contact_Type__c)){
                if(c.AccountId!= Trigger.oldMap.get(c.Id).AccountId ){
                    AccID.add(c.AccountId);
                }
             // for old contacts with updated contact Type
                if(c.AccountId == Trigger.oldMap.get(c.Id).AccountId && c.Contact_Type__c!=Trigger.oldMap.get(c.Id).Contact_Type__c){
                    AccID.add(c.AccountId);
                }
            }
            oldAccId.add(Trigger.oldMap.get(c.Id).AccountId);
        }
        If(!AccID.isEmpty()){
            //for new Accounts
            List<Account> acctList = [Select id,Member_CA__c,Member_FR__c,Member_JP__c,(Select id,Contact_Type__c from Contacts) from Account Where Id in :AccID];
            //for new contact accounts
            List<Contact> conList = [Select id,Contact_Type__c from Contact Where AccountId in : AccID];
            for(Contact con : conList){
                If(con.Contact_Type__c == 'Canadian'){
                conCanadianList.add(con);
            }
            If(con.Contact_Type__c == 'French'){
                conFrenchList.add(con);
            }
            If(con.Contact_Type__c == 'Japanese'){
                conJapaneseList.add(con);
            }
        }
        update conCanadianList;
        update conFrenchList;
        update conJapaneseList;
        /* This is For Old Contacts Count */
            // for old Account
             List<Account> oldacctList = [Select id,Member_CA__c,Member_FR__c,Member_JP__c,(Select id,Contact_Type__c from Contacts) from Account Where Id in :oldAccId];
            // for old contact account
            
             List<Contact> oldconCanada = [Select id,Contact_Type__c from Contact Where AccountId in : oldAccId and Contact_Type__c = 'Canadian'];
             List<Contact> oldconFrench = [Select id,Contact_Type__c from Contact Where AccountId in : oldAccId and Contact_Type__c = 'French'];
             List<Contact> oldconJapanese = [Select id,Contact_Type__c from Contact Where AccountId in : oldAccId and Contact_Type__c = 'Japanese'];
            
            for(Account a : acctList){
                a.Member_CA__c = conCanadianList.size();
                a.Member_FR__c = conFrenchList.size();
                a.Member_JP__c = conJapaneseList.size();
            }
            try{
                update acctList;
            }
            catch(DMLException e){
                for(contact con : Trigger.new){
                    con.addError(e.getDmlMessage(0));
                }
            }
            for(Account a : oldacctList){
                a.Member_CA__c = oldconCanada.size();
                a.Member_FR__c = oldconFrench.size();
                a.Member_JP__c = oldconJapanese.size();
            }
            try{
                update oldacctList;
            }
            catch(DMLException e){
                for(Contact con : Trigger.new){
                    con.addError(e.getDmlMessage(0));
                }
            }
         }
      }
}