I have this monster of a query:

List<Case> updatedCases = [SELECT Id,ProductId, Vehicle__c, 
                               (SELECT Equipment__r.Maintenance_Cycle__c FROM Equipment_Maintenance_Items__r ORDER BY 
                                Equipment__r.Maintenance_Cycle__c ASC NULLS LAST LIMIT 1) FROM Case WHERE Id 
                             IN :cases AND (Type = 'Repair' OR Type = 'Routine Maintenance') AND 
                              Status = 'Closed'];

And I need to assign the value of the Maintenance_Cycle__c to a new case (in the Date_Due__c field) like so (except this one doesn't work):

 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();
        b.Date_Due__c = Date.today() + updatedCases.get(Equipment__r.Maintenance_Cycle__c);
    insert newCases;

How would I retrieve that value from my SOQL?

1 Answer 1


You can use the value from a:

if(a.Equipment_Maintenance_Items__r.size() == 1) {
  a.Due_Date__c = Date.today() + a.Equipment_Maintenance_Items__r[0].Equipment__r.Maintenance_Cycle__c;
  • Thank you! I had to research some more to get the following code that executes without any error: if(a.Equipment_Maintenance_Items__r.size() == 1) { Decimal numV = a.Equipment_Maintenance_Items__r[0].Equipment__r.Maintenance_Cycle__c; Integer cycle = numV.intValue(); b.Date_Due__c = b.Date_Reported__c.addDays(cycle); } `
    – ApexNoob
    Feb 26 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.