I am trying to return the SOQL listed values into a field using an Apex Trigger:-
Object:
- Name =
RPA__c
Fields:
Business_Area__c
(picklist) VALUES = Finland Managed Services, UK Managed ServicesBusiness_ID_Number_HIDDEN__c
(Number field) VALUES = integer
SOQL:
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)
BUT
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!
RPA__c
records rolling up onto eachRPA__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?