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
Alex Raine 9Alex Raine 9 

Get count of records based on field value

Hi All,

I was wondering if anyone would be able to assist me with the below:

I have a custom object called Emergecny Stock Order which has a child object called Emergecny Stock Order Lines that has a field called Product

I need to use apex code to group all the order lines related to the same emergency stock order by Product. I then need to be able to identify how many records are within each group (i.e how many order lines have the same product and are related to the same emergency stock order). This is so i cna perform a particular action if a group contians more than 1 record.

Regards
Alain CabonAlain Cabon
@Alex Raine

Emergency_Stock_Order__c
Emergency_Stock_Order_Lines__c
Product1__c 
 
select Emergency_Stock_Order__r.Name EmergencyStockOrderName, Product1__r.Name ProductName, count(Emergency_Stock_Order_Lines__c.Id), count(Product1__r.Id), count(Emergency_Stock_Order__r.Id)
from Emergency_Stock_Order_Lines__c
group by Emergency_Stock_Order__r.Name,Product1__r.Name