I am trying to find a minimum value of the field Maintenance_Cycle__c (which is a field on the Product standard object which is looked up by the Equipment_Maintenance_Item__c custom object, which is in turn a child of the Case standard object through master-detail relationship) and then assign it to another field in a new Case record.
My code is as follows:
public with sharing class MaintenanceRequestHelper {
public static void updateWorkOrders(List<Case> cases) {
List<Case> updatedCases = [SELECT Id,ProductId, Vehicle__c, (SELECT MIN(Equipment__r.Maintenance_Cycle__c) FROM Equipment_Maintenance_Items__r) FROM Case WHERE Id
IN :cases AND (Type = 'Repair' OR Type = 'Routine Maintenance') AND
Status = 'Closed'];
List<Case> newCases = new List<Case>();
for(Case a:updatedCases){
Case b = new Case();
b.Type = 'Routine Maintenance';
b.Vehicle__c = a.Vehicle__c;
b.Subject = 'Routine Maintenance Request';
b.Date_Reported__c = Date.today();
//to do: Date_Due__c field
newCases.add(b);
}
insert newCases;
}
I already have an error with this code "Grouped query cannot use child relationships in the SELECT list", which is caused by the MIN function (there is no error without it). I am also doing a MIN function because there will be several values and I need to find the smallest one. I will then need to assign that value somehow to the Date_Due__c field, but I do not even know how to access that value from the query, since it is few relationships away.