• Michael Lau
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
Good day guys, still learning and quite new, but I was just wondering what's wrong with my code.. when I run it it seems to do what is required by the challenge, but it's returning an error that says closing a routine maintenance type maintenance request is not creating a new maintenance request with same vehicle, but when I ran my code it did? Here's my code...
 
trigger:
trigger MaintenanceRequest on Case (before update, after update) {
    // ToDo: Call MaintenanceRequestHelper.updateWorkOrders
    if(Trigger.isUpdate) {
        if (Trigger.isAfter) {
            MaintenanceRequestHelper.updateWorkOrders(Trigger.New);
        }
    }
}

apex:
public with sharing class MaintenanceRequestHelper {
    public static void updateWorkOrders(List<Case> updatedCase) {

        for(Case a : updatedCase) {
            if (a.Status == 'Closed' && (a.Type == 'Repair' || a.Type == 'Routine Maintenance')) {
                
                helperBatch myBatchObject = new helperBatch(updatedCase);
                Id batchId = Database.executeBatch(myBatchObject);
            }
        }
        
    }          
}

helper apex batch:
public class helperBatch implements
Database.Batchable<sObject>, Database.Stateful {
    // instance member to retain state across transactions
    public Integer recordsProcessed = 0;
    public List<Case> updatedCase = new List<Case>();
    
    public helperBatch(List<Case> temp) {
        updatedCase = temp;
    }
    
    public Database.QueryLocator start(Database.BatchableContext bc) {
        system.debug('yo: ' + updatedCase);
        return Database.getQueryLocator(
            'SELECT Equipment__c, Name FROM Equipment_Maintenance_Item__c WHERE Maintenance_Request__c IN :updatedCase'
        );
    }
    public void execute(Database.BatchableContext bc, List<Equipment_Maintenance_Item__c> scope){
        // process each batch of records
        Date dueDate = null;
        List<Product2> cycle = new List<Product2>(); 
        List<id> equipments = new List<id>();
        List<Equipment_Maintenance_Item__c> finalEquipment = new List<Equipment_Maintenance_Item__c>();
        set<Id> equipId = new Set<Id>();
        Vehicle__c vehicle = new Vehicle__c();
        
        for(Case a : updatedCase) {
            vehicle.id = a.Vehicle__c;    
            /*
if (a.Vehicle__c != '') {
vehicle.id = a.Vehicle__c;    
} 
else {
vehicle.id = '';
}*/
        }
        
        system.debug('before count');
        
        List<Equipment_Maintenance_Item__c> listEquipments = new List<Equipment_Maintenance_Item__c>([SELECT Equipment__c, Name, Quantity__c FROM Equipment_Maintenance_Item__c WHERE Maintenance_Request__c IN :updatedCase]);
        system.debug('list: ' + listEquipments);
        Integer x = listEquipments.size();        
        
        system.debug('equip size: ' + x);
        
        Case newCase = new Case(
            Type = 'Routine Maintenance',
            Vehicle__c = updatedCase[0].Vehicle__c,
            //Vehicle__c = vehicle.Id,
            Subject = updatedCase[0].Subject,
            Date_Reported__c = Date.today(),
            Status = 'Open',
            Origin = 'Web'
        );
        
        system.debug('id: ' + updatedCase[0].Id);
        
        system.debug('before upsert');
        upsert newCase;  
        system.debug('after upsert');
        if (x > 0) {
            for (Integer i = 0; i < x; i++) {
                
                equipments.add(listEquipments[i].Equipment__c);
                
                finalEquipment.add(new Equipment_Maintenance_Item__c(
                    Equipment__c = equipments[i],
                    Maintenance_Request__c = newCase.Id,
                    Quantity__c = listEquipments[i].Quantity__c)
                                  );
                
                equipId.add(equipments[i]);
            }
            
            upsert finalEquipment;
            
        }
        
        cycle = [select Maintenance_Cycle__c from Product2 where Product2.id in :equipId order by Maintenance_Cycle__c ASC];
        
        
        
        system.debug('cycle size: ' + cycle.size());
        dueDate = Date.today();
        if(cycle.size() > 0) {
            dueDate = dueDate.addDays(Integer.valueOf(cycle[0].Maintenance_Cycle__c));    
        }
        else {
            dueDate = Date.today();
        }
        
        newCase.Date_Due__c = dueDate;
        update newCase;
        system.debug(newCase);
        system.debug(finalEquipment);
    }
    
    public void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed. Shazam!');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
                            JobItemsProcessed,
                            TotalJobItems, CreatedBy.Email
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];
        
        system.debug('job: ' + job);
    }
}

Thanks in advance.. what am I doing wrong?
Good day guys, still learning and quite new, but I was just wondering what's wrong with my code.. when I run it it seems to do what is required by the challenge, but it's returning an error that says closing a routine maintenance type maintenance request is not creating a new maintenance request with same vehicle, but when I ran my code it did? Here's my code...
 
trigger:
trigger MaintenanceRequest on Case (before update, after update) {
    // ToDo: Call MaintenanceRequestHelper.updateWorkOrders
    if(Trigger.isUpdate) {
        if (Trigger.isAfter) {
            MaintenanceRequestHelper.updateWorkOrders(Trigger.New);
        }
    }
}

apex:
public with sharing class MaintenanceRequestHelper {
    public static void updateWorkOrders(List<Case> updatedCase) {

        for(Case a : updatedCase) {
            if (a.Status == 'Closed' && (a.Type == 'Repair' || a.Type == 'Routine Maintenance')) {
                
                helperBatch myBatchObject = new helperBatch(updatedCase);
                Id batchId = Database.executeBatch(myBatchObject);
            }
        }
        
    }          
}

helper apex batch:
public class helperBatch implements
Database.Batchable<sObject>, Database.Stateful {
    // instance member to retain state across transactions
    public Integer recordsProcessed = 0;
    public List<Case> updatedCase = new List<Case>();
    
    public helperBatch(List<Case> temp) {
        updatedCase = temp;
    }
    
    public Database.QueryLocator start(Database.BatchableContext bc) {
        system.debug('yo: ' + updatedCase);
        return Database.getQueryLocator(
            'SELECT Equipment__c, Name FROM Equipment_Maintenance_Item__c WHERE Maintenance_Request__c IN :updatedCase'
        );
    }
    public void execute(Database.BatchableContext bc, List<Equipment_Maintenance_Item__c> scope){
        // process each batch of records
        Date dueDate = null;
        List<Product2> cycle = new List<Product2>(); 
        List<id> equipments = new List<id>();
        List<Equipment_Maintenance_Item__c> finalEquipment = new List<Equipment_Maintenance_Item__c>();
        set<Id> equipId = new Set<Id>();
        Vehicle__c vehicle = new Vehicle__c();
        
        for(Case a : updatedCase) {
            vehicle.id = a.Vehicle__c;    
            /*
if (a.Vehicle__c != '') {
vehicle.id = a.Vehicle__c;    
} 
else {
vehicle.id = '';
}*/
        }
        
        system.debug('before count');
        
        List<Equipment_Maintenance_Item__c> listEquipments = new List<Equipment_Maintenance_Item__c>([SELECT Equipment__c, Name, Quantity__c FROM Equipment_Maintenance_Item__c WHERE Maintenance_Request__c IN :updatedCase]);
        system.debug('list: ' + listEquipments);
        Integer x = listEquipments.size();        
        
        system.debug('equip size: ' + x);
        
        Case newCase = new Case(
            Type = 'Routine Maintenance',
            Vehicle__c = updatedCase[0].Vehicle__c,
            //Vehicle__c = vehicle.Id,
            Subject = updatedCase[0].Subject,
            Date_Reported__c = Date.today(),
            Status = 'Open',
            Origin = 'Web'
        );
        
        system.debug('id: ' + updatedCase[0].Id);
        
        system.debug('before upsert');
        upsert newCase;  
        system.debug('after upsert');
        if (x > 0) {
            for (Integer i = 0; i < x; i++) {
                
                equipments.add(listEquipments[i].Equipment__c);
                
                finalEquipment.add(new Equipment_Maintenance_Item__c(
                    Equipment__c = equipments[i],
                    Maintenance_Request__c = newCase.Id,
                    Quantity__c = listEquipments[i].Quantity__c)
                                  );
                
                equipId.add(equipments[i]);
            }
            
            upsert finalEquipment;
            
        }
        
        cycle = [select Maintenance_Cycle__c from Product2 where Product2.id in :equipId order by Maintenance_Cycle__c ASC];
        
        
        
        system.debug('cycle size: ' + cycle.size());
        dueDate = Date.today();
        if(cycle.size() > 0) {
            dueDate = dueDate.addDays(Integer.valueOf(cycle[0].Maintenance_Cycle__c));    
        }
        else {
            dueDate = Date.today();
        }
        
        newCase.Date_Due__c = dueDate;
        update newCase;
        system.debug(newCase);
        system.debug(finalEquipment);
    }
    
    public void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed. Shazam!');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
                            JobItemsProcessed,
                            TotalJobItems, CreatedBy.Email
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];
        
        system.debug('job: ' + job);
    }
}

Thanks in advance.. what am I doing wrong?