• Abhi92
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies
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));
                }
            }
         }
      }
}

 
  • November 18, 2020
  • Like
  • 0
I've created a below validation rule on Account, but it's not firing when the length of the Account number is not 8, record getting saved even length is 5 number. What wrong I'm doing ??

AND(
ISBLANK(AccountNumber),
NOT(ISNUMBER(AccountNumber)),
LEN(AccountNumber) <> 8
)
  • October 27, 2020
  • Like
  • 1
Hi,

I'm creating Custom Email Template(Text) for Approval aprocess in order to get email notifications. But instead of populating approver name in first line after Hi "ApproverName" it's priniting the requester name who is submitting the approval request. Below is the email template,

Hi {!ApprovalRequest.Process_Approver},

{!Approval_Requesting_User.Name} has requested your approval to create this project in Mavenlink : {!Opportunity.Name}.

Please approve that the Opportunity naming convention is correct (Account Abbreviation - Opportunity Name - Sitecore or SF), Contacts are added to the Opportunity and the estimate and outline have been added.

Please visit the link below and either approve or reject it.
{!Opportunity.Link}

Thanks,
 
  • September 18, 2020
  • Like
  • 0
I've created a below validation rule on Account, but it's not firing when the length of the Account number is not 8, record getting saved even length is 5 number. What wrong I'm doing ??

AND(
ISBLANK(AccountNumber),
NOT(ISNUMBER(AccountNumber)),
LEN(AccountNumber) <> 8
)
  • October 27, 2020
  • Like
  • 1
I've created a below validation rule on Account, but it's not firing when the length of the Account number is not 8, record getting saved even length is 5 number. What wrong I'm doing ??

AND(
ISBLANK(AccountNumber),
NOT(ISNUMBER(AccountNumber)),
LEN(AccountNumber) <> 8
)
  • October 27, 2020
  • Like
  • 1
Hi,

I'm creating Custom Email Template(Text) for Approval aprocess in order to get email notifications. But instead of populating approver name in first line after Hi "ApproverName" it's priniting the requester name who is submitting the approval request. Below is the email template,

Hi {!ApprovalRequest.Process_Approver},

{!Approval_Requesting_User.Name} has requested your approval to create this project in Mavenlink : {!Opportunity.Name}.

Please approve that the Opportunity naming convention is correct (Account Abbreviation - Opportunity Name - Sitecore or SF), Contacts are added to the Opportunity and the estimate and outline have been added.

Please visit the link below and either approve or reject it.
{!Opportunity.Link}

Thanks,
 
  • September 18, 2020
  • Like
  • 0