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
Shruthi MN 88Shruthi MN 88 

Database.update

Hi Everyone,

Can you help be with the below code:
1. Create an Account Record with Name =”Eternus”. Create associated contacts. Create a Custom field called Contact Count on Account . Query on Contact
where Account.Name =”Eternus” and count the associated contacts. Update the custom field on Accounts with that count..

I am not getting the count of the records:

public class Accassoccon {
    public static void accountacc(){
        Account a = new Account();
        a.Name = 'Eternus';
        Database.insert(a,false);
        system.debug('The inserted account is:' +a);
       list<Account > recordIDs = new   list<Account >();
List<Account> accountlist1 = [Select Id, ContactCount__c, (Select Id From Contacts) From Account where Id =: recordIDs];  //get a list of the corresponding accounts
for(Account account : accountlist1) {
    
    if(account.Contacts == null) continue;
    
    account.ContactCount__c = account.Contacts.size();
}

Database.update(accountList1,false);        
    }

}
Srikanth BanothSrikanth Banoth
you did not create any associated contacts in the code you provided that is why it's showing you 0 as count 
SwethaSwetha (Salesforce Developers) 
Try this updated code:
public class Accassoccon {
    public static void accountacc() {
        // Create the Account record
        Account a = new Account();
        a.Name = 'Eternus';
        Database.insert(a, false);
        System.debug('The inserted account is: ' + a);

        // Query for Contacts associated with the Account
        List<Account> accountList = [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Name = 'Eternus'];

        if (!accountList.isEmpty()) {
            Account account = accountList[0]; // Assuming there's only one 'Eternus' account; you may want to add additional checks if needed

            // Get the count of associated Contacts
            Integer contactCount = account.Contacts.size();

            // Update the custom field on the Account with the contact count
            account.ContactCount__c = contactCount;
            
            // Update the Account record
            Database.update(account, false);
        }
    }
}

If this information helps, please mark the answer as best. Thank you
Arun Kumar 1141Arun Kumar 1141

Hi Shruthi,

Try with this:

public class Accassoccon {
    public static void accountacc(){
        // Create an Account record
        Account a = new Account();
        a.Name = 'Eternus';
        Database.insert(a,false);
        system.debug('The inserted account is: ' + a);

        // Create associated Contacts
        Contact c1 = new Contact();
        c1.LastName = 'Contact1';
        c1.AccountId = a.Id;
        Contact c2 = new Contact();
        c2.LastName = 'Contact2';
        c2.AccountId = a.Id;
        List<Contact> contacts = new List<Contact>{c1, c2};
        Database.insert(contacts, false);

        // Query the Account and count associated Contacts
        List<Account> accountList = [SELECT Id, ContactCount__c, (SELECT Id FROM Contacts) FROM Account WHERE Name = 'Eternus'];

        for (Account account : accountList) {
            if (account.Contacts != null) {
                account.ContactCount__c = account.Contacts.size();
            } else {
                account.ContactCount__c = 0;
            }
        }

        // Update the Account records
        if (!accountList.isEmpty()) {
            Database.update(accountList, false);
        }
    }
}

I hope this will help you.
Thanks!