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
Christine KleinChristine Klein 

Bulk Updates

Below is a snippet of code which counts the number of users that have been added to a custom object on Accounts.

List<AggregateResult> numOfOwners = [select count(Account_Owner__c)cnt,Account_Name__c from Account_Coverage__c WHERE Account_Name__c in : accts AND (NOT MU__c like 'Market Development') and (NOT MU__c like 'Professional Services') and Coverage__c = true GROUP BY Account_Name__c];
  		decimal decimalResults = 0;
  		
  		if(numOfOwners.size() > 0)
  		{
  			String numOfOwnersStr = '' + numOfOwners[0].get('cnt');
  			decimalResults = Decimal.ValueOf(numOfOwnersStr);
  			System.debug('decimalResults = ' + decimalResults);
  		}

I didn't notice an issue with the above code until I tried mass inserting a bunch of owners in the custom object.  If one user was added through DemandTools, # of Owners would display 89.  If I manually add users to the custom object the # of Owners field calculates correctly.

I was just wondering how I can 'bulkify' the above so when I mass insert records the # of Owners field will display the correct count.

Thanks in advance!


Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Like trigger.new will contain new record.so trigger.new.size() will get the count of records that was inserted in the account