I am trying to return the SOQL listed values into a field using an Apex Trigger:-


  1. Name = RPA__c


  1. Business_Area__c (picklist) VALUES = Finland Managed Services, UK Managed Services
  2. Business_ID_Number_HIDDEN__c (Number field) VALUES = integer


List<AggregateResult> lstResult =   [SELECT Business_Area__c value, Count(Id)  
                                    FROM Records_of_Processing_Activities__c 
                                    GROUP BY Business_Area__c];

So, the SOQL is returning as below:-

**Business Area                Count(id)**
Finland Managed Services       5
UK Managed Services            3

This is working perfectly and returns the correct count of the records where "Business Area" contains "Finland Managed Services" (3)


I am really struggling to capture that Count(id) and add it into a field in the RPA__c object (Business_ID_Number_HIDDEN__c is the field to apply the value into)

So:- I have written this so far, but is not returning the count(id) into the Business_Area__c field:-

trigger BATrigger on RPA__c (after insert, after update) 
    List<AggregateResult> lstResult =   [SELECT Business_Area__c value, Count(Id)  
                                        FROM RPA__c 
                                        GROUP BY Business_Area__c];
    List<Records_of_Processing_Activities__c> Business_ID_Number_HIDDEN__c = new List<Records_of_Processing_Activities__c>();
    for(AggregateResult result:lstResult);
if (lstResult == 'Finland Managed Services') {
    Business_ID_Number_HIDDEN__c = Count(Id);
} else if (lstResult == 'UK Managed Services') {
    Business_ID_Number_HIDDEN__c = Count(Id);
} else {
    Business_ID_Number_HIDDEN__c = null;
    update Business_ID_Number_HIDDEN__c; 

Any help or guidance would be greatly appreciated, again, very new to this so please be constructive in your appreciated help!

  • 2
    Hi Andy, welcome to SFSE. It's a bit unusual to roll up a record count onto the record itself - here you have counts of RPA__c records rolling up onto each RPA__c records. That structure can cause some problems. Have you considered approaching this need with a Summary Report? What's driving this data model choice?
    – David Reed
    Feb 18, 2019 at 16:48
  • Hi David,thanks for the quick response. Yes, it's indeed a tough one... in simple terms, we need to have a unique ID that is associated against each record, this will need to be derived from the above (poor) attempt :) I will try to articulate the expected results below:- Feb 18, 2019 at 17:04
  • we cannot use Master Detail (as its a single object needed and no parent child, we cannot use a filter/condition on AutoNumber fields (SFDC limitation) and must have each Business area "count() sequention based on the picklist value, not the record count (if only life was that easy!!) Feb 18, 2019 at 17:07
  • hence the sequential value against each record based on the Business Area and # of records where Business area = Finland Managed services OR UK Managed Services. 2 days into Apex and counting!! :D Feb 18, 2019 at 17:09
  • It would be helpful to add all of that relevant detail to the body of your question. Filtered auto-numbers come up a lot; this question is probably a duplicate: salesforce.stackexchange.com/questions/157225/…
    – David Reed
    Feb 18, 2019 at 17:36

1 Answer 1


The default way to access espressions is expr0 for the first, expr1 for the second, and so on. A preferable way for most is to use an alias.

Adding an alias would work as below.

SELECT Business_Area__c, count(Id) recordCount
FROM MyObject__c WHERE ...

The actual retrieval of this attribute would work as below.

Without Alias

Integer count = result.get('expr0');

With Alias

Integer count = result.get('recordCount');
  • Thanks for this Adrian, will try it now... Feb 18, 2019 at 17:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .