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
Zabi noorZabi noor 

Rollup the USA contact count on the Account field and add to list<contact>

rigger ContactTrigger on Contact (after insert, after update, after delete, after undelete) {

    //insert --> trigger.new
    //update --> trigger.new 
    //delete --> trigger.old 
    //undelete --> trigger.new 

    Set<Id> accIds = new Set<Id>();
    for(Contact con : trigger.isDelete ? trigger.old : trigger.new) {
        accIds.add(con.AccountId);
    }

    Map<Id,Integer> accIdConCountMap = new Map<Id,Integer>();
    Map<Id,List<Contact>> accIdConLstMap = new Map<Id,List<Contact>>();

    for(Contact con : [SELECT Id FROM Contact WHERE AccountId in: accIds AND MailingCountry = 'USA']) {

    }

}
AnudeepAnudeep (Salesforce Developers) 
Hi Zabi, 

Posting here a sample code. This will add the contact's Custom field and shows Total on Accounts
 
trigger ContactSumTrigger on Contact (after delete, after insert, after undelete, after update) {

Contact[] cons;
if (Trigger.isDelete) 
    cons = Trigger.old;
else
    cons = Trigger.new;

Set<ID> acctIds = new Set<ID>();
for (Contact con : cons) {
   acctIds.add(con.AccountId);
}

Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([SELECT Id ,AccountId, Field_On_Contact__c FROM Contact WHERE AccountId IN :acctIds]);

Map<ID, Account> acctsToUpdate = new Map<ID, Account>([SELECT Id, Field_On_Account__c FROM Account WHERE Id IN :acctIds]);

for (Account acct : acctsToUpdate.values()) {
Set<Id> conIds = new Set<Id>();
Decimal totalValue = 0;
for (Contact con : contactsForAccounts.values()) {
    if (con.AccountId == acct.Id && con.Field_On_Contact__c != NULL) {
        totalValue += con.Field_On_Contact__c; 
    }
}
acc.Field_On_Account__c = totalValue;
}
if(acctsToUpdate.values().size() > 0) {
    update acctsToUpdate.values();
}
}

NOTE: The code provided is an example. You'll need to review and make modifications for your organization.

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you