• Veera Lakshmi Vaidadi
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
I'm getting the following error;
"Challenge Not yet complete... here's what's wrong: 
Inserting a new Maintenance Request of type 'Routine Maintenance' and then closing it did not create of a new Maintenance Request based upon the original record correctly. The challenge is expecting to find the closed Maintenance Request plus an 'New' Maintenance Request of type 'Routine Maintenance' with the same Vehicle as the closed one."
I have had a look at the forum links you provided in a previous post but it's not resolving.

I have added 'Routine Maintenance' and 'Repair' in Case field Type. (Why should this be needed and this type is inconsistent with the other picklist types e.g Electrical, Structural etc)
I also include a filed 'Old Case' to store the Case ID once closed. The challenge is to automate through writing the trigger and the helkper class right? No test class is required at this stage?
Here is my code;
___________________________________________________________
trigger MaintenanceRequest on Case (before update, after update) {  
    List<case>ClosedCaseList = [SELECT Id, subject,Reason, Vehicle__c, vehicle__r.Name, equipment__c,type 
                                FROM Case WHERE Id IN :Trigger.New 
                                AND (status = 'closed' AND (type = 'Repair' OR type = 'Routine Maintenance'))];   
    Map<Id, Case>CasesToConsider = new Map<Id, Case>();
    for(Case c: ClosedCaseList){
        Boolean alreadyClosed = Trigger.oldMap.get(c.Id).Status.equals('Closed') ;
        if(!alreadyClosed)
        {
            CasesToConsider.put(c.id, c);
        }
    }
    
    List<Work_Part__c> allWorkParts = [SELECT ID, Equipment__c, Maintenance_Request__c, 
                Quantity__c, Equipment__r.Maintenance_Cycle__c FROM Work_Part__c 
                WHERE Maintenance_Request__c in: CasesToConsider.keySet()];
    
    MaintenanceRequestHelper.updateWorkOrders(CasesToConsider, allWorkParts);       
}

___________________________________________________________
public class MaintenanceRequestHelper {
    
    public static void updateWorkOrders(List<Case> maintenanceRequests){

        Set<Case> targetRequests = new Set<Case>();
        for (Case mr : maintenanceRequests)
            if (('Repair' == mr.Type || 'Routine Maintenance' == mr.Type) && 'Closed' == mr.Status)
                targetRequests.add(mr);

        List<AggregateResult> results = [
            SELECT Maintenance_Request__c, MIN(Equipment__r.Maintenance_Cycle__c)
            FROM Work_Part__c
            WHERE Maintenance_Request__c IN :targetRequests
            GROUP BY Maintenance_Request__c
        ];

        Map<Id, Integer> requestToMinCycle = new Map<Id, Integer>();
        for (AggregateResult ar : results)
            requestToMinCycle.put(
                (Id)ar.get('Maintenance_Request__c'),
                ((Double)ar.get('expr0')).intValue()
            );

        List<Case> workOrders = new List<Case>();
        for (Case t : targetRequests)
            if (('Repair' == t.Type || 'Routine Maintenance' == t.Type) && 'Closed' == t.Status)
                workOrders.add(new Case(
                    Vehicle__c=t.Vehicle__c,
                    Type='Routine Maintenance',
                    Subject=t.Subject,
                    Status='New',
                    Date_Reported__c=Date.today(),
                    Date_Due__c=Date.today().addDays(requestToMinCycle.get(t.Id))
                ));
        
        if (!workOrders.isEmpty())
            insert workOrders;
    } 
}
Challenge Not yet complete... here's what's wrong: 
The runWarehouseEquipmentSync method does not appear to have run successfully. Could not find a successfully completed @future job for this method. Make sure that you run this method at least one before attempting this challenge. Since this method is annotated with the @future method, you may want to wait for a few seconds to ensure that it has processed successfully.

here is my code

public with sharing class WarehouseCalloutService {
    
    private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
    @future(callout=true)
    // complete this method to make the callout (using @future) to the
    // REST endpoint and update equipment on hand.
    public static void runWarehouseEquipmentSync(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(WAREHOUSE_URL);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        // If the request is successful, parse the JSON response.
        String ret = '';
        if (response.getStatusCode() == 200) {
            // Deserialize the JSON string into collections of primitive data types.
            List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
            
            List<Product2> LstProduct = new List<Product2>();
            for(Object obj : results)
            { 
                Map<String, Object> mapobj = (Map<String, Object>)obj;
                Product2 product = new Product2();
                           
                Integer maintenanceperiod = (Integer)mapobj.get('maintenanceperiod');               
                Integer Lifespan = (Integer)mapobj.get('lifespan');
                Integer Cost = (Integer)mapobj.get('cost');
                Boolean replacement = (Boolean)mapobj.get('replacement');
                Integer quantity = ((Integer)mapobj.get('qIntegerntity'));
                product.Name  = (String)mapobj.get('name'); 
                product.Maintenance_Cycle__c = Integer.valueof(maintenanceperiod);
                product.Cost__c = Cost;
                product.Current_Inventory__c = quantity;
                product.Lifespan_Months__c = Lifespan; 
                product.Replacement_Part__c = replacement;
                product.Warehouse_SKU__c = (String) mapobj.get('sku');
                product.ProductCode = (String)mapobj.get('_id');       
                LstProduct.add(product);
            }
            
            System.debug(LstProduct);
             upsert LstProduct Warehouse_SKU__c;
        
        }
        
       
    }
}