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
Prasanth RPrasanth R 

apex superbadge challenge2 npt cpmplete

i'm doing the apex specialist superbadge
https://trailhead.salesforce.com/en/content/learn/superbadges/superbadge_apex

i'm not able to clear the challenge kindly someone help
WarehouseCalloutService:apxc:
public with sharing class WarehouseCalloutService implements Queueable {
    private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
    
    //class that makes a REST callout to an external warehouse system to get a list of equipment that needs to be updated.
    //The callout’s JSON response returns the equipment records that you upsert in Salesforce. 
    
    @future(callout=true)
    public static void runWarehouseEquipmentSync(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        
        request.setEndpoint(WAREHOUSE_URL);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        List<Product2> warehouseEq = new List<Product2>();
        
        if (response.getStatusCode() == 200){
            List<Object> jsonResponse = (List<Object>)JSON.deserializeUntyped(response.getBody());
            System.debug(response.getBody());
            
            //class maps the following fields: replacement part (always true), cost, current inventory, lifespan, maintenance cycle, and warehouse SKU
            //warehouse SKU will be external ID for identifying which equipment records to update within Salesforce
            for (Object eq : jsonResponse){
                Map<String,Object> mapJson = (Map<String,Object>)eq;
                Product2 myEq = new Product2();
                myEq.Replacement_Part__c = (Boolean) mapJson.get('replacement');
                myEq.Name = (String) mapJson.get('name');
                myEq.Maintenance_Cycle__c = (Integer) mapJson.get('maintenanceperiod');
                myEq.Lifespan_Months__c = (Integer) mapJson.get('lifespan');
                myEq.Cost__c = (Integer) mapJson.get('cost');
                myEq.Warehouse_SKU__c = (String) mapJson.get('sku');
                myEq.Current_Inventory__c = (Double) mapJson.get('quantity');
                myEq.ProductCode = (String) mapJson.get('_id');
                warehouseEq.add(myEq);
            }
            
            if (warehouseEq.size() > 0){
                upsert warehouseEq;
                System.debug('Your equipment was synced with the warehouse one');
            }
        }
    }
    
    public static void execute (QueueableContext context){
        runWarehouseEquipmentSync();
    }
    
}

anonymous window:
WarehouseCalloutService.runWarehouseEquipmentSync();
User-added imageUser-added image
 
mukesh guptamukesh gupta
Hi Prasanth,

Please use below apex class code:- 

WarehouseCalloutService
public with sharing class WarehouseCalloutService {

    private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
    
    //@future(callout=true)
    public static void runWarehouseEquipmentSync(){
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        
        request.setEndpoint(WAREHOUSE_URL);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        
        List<Product2> warehouseEq = new List<Product2>();
        
        if (response.getStatusCode() == 200){
            List<Object> jsonResponse = (List<Object>)JSON.deserializeUntyped(response.getBody());
            System.debug(response.getBody());
            
            for (Object eq : jsonResponse){
                Map<String,Object> mapJson = (Map<String,Object>)eq;
                Product2 myEq = new Product2();
                myEq.Replacement_Part__c = (Boolean) mapJson.get('replacement');
                myEq.Name = (String) mapJson.get('name');
                myEq.Maintenance_Cycle__c = (Integer) mapJson.get('maintenanceperiod');
                myEq.Lifespan_Months__c = (Integer) mapJson.get('lifespan');
                myEq.Cost__c = (Decimal) mapJson.get('lifespan');
                myEq.Warehouse_SKU__c = (String) mapJson.get('sku');
                myEq.Current_Inventory__c = (Double) mapJson.get('quantity');
                warehouseEq.add(myEq);
            }
            
            if (warehouseEq.size() > 0){
                upsert warehouseEq;
                System.debug('Your equipment was synced with the warehouse one');
                System.debug(warehouseEq);
            }
            
        }
    }
}
if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 
Prasanth RPrasanth R
@mukesh still having the same problem used your above code..please help me to finish this challenge

anoynous window : 
WarehouseCalloutService.runWarehouseEquipmentSync();

User-added imageUser-added imageUser-added image
mukesh guptamukesh gupta
Hi Prashant,

Please open the Future method that's you have commented. 

After saving the code open execute anonymous window ( CTRl+E ) and run this method :-

System.enqueueJob(new WarehouseCalloutService());

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 


 
Prasanth RPrasanth R
@mukesh gupta still facing the problem User-added image
AbhinavAbhinav (Salesforce Developers) 
Please note that Questions about how to pass Trailhead challenges are not on topic, because these challenges are intended to be independent demonstrations of your abilities.

Trailhead Help (https://trailhead.salesforce.com/en/help?support=home)can provide assistance for situations where Trailhead does not appear to be functioning correctly. You can reach out to them if this is the case.

Thanks!
mukesh guptamukesh gupta
Hi Pershanth,

Please use below code:- 

WarehouseCalloutService
public with sharing class WarehouseCalloutService implements Queueable {

    private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
    
    //@future(callout=true)
    public static void runWarehouseEquipmentSync(){
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        
        request.setEndpoint(WAREHOUSE_URL);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        
        List<Product2> warehouseEq = new List<Product2>();
        
        if (response.getStatusCode() == 200){
            List<Object> jsonResponse = (List<Object>)JSON.deserializeUntyped(response.getBody());
            System.debug(response.getBody());
            
            for (Object eq : jsonResponse){
                Map<String,Object> mapJson = (Map<String,Object>)eq;
                Product2 myEq = new Product2();
                myEq.Replacement_Part__c = (Boolean) mapJson.get('replacement');
                myEq.Name = (String) mapJson.get('name');
                myEq.Maintenance_Cycle__c = (Integer) mapJson.get('maintenanceperiod');
                myEq.Lifespan_Months__c = (Integer) mapJson.get('lifespan');
                myEq.Cost__c = (Decimal) mapJson.get('lifespan');
                myEq.Warehouse_SKU__c = (String) mapJson.get('sku');
                myEq.Current_Inventory__c = (Double) mapJson.get('quantity');
                warehouseEq.add(myEq);
            }
            
            if (warehouseEq.size() > 0){
                upsert warehouseEq;
                System.debug('Your equipment was synced with the warehouse one');
                System.debug(warehouseEq);
            }
            
        }
    }
	
	  public static void execute (QueueableContext context){
        runWarehouseEquipmentSync();
    }
}

After saving the code open execute anonymous window ( CTRl+E ) and run this method :-

System.enqueueJob(new WarehouseCalloutService());


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 
mukesh guptamukesh gupta
Hi Pershant,

I hope your issue has resolved by this solution. 

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
Prasanth RPrasanth R
mukesh completed challenge 2,I'm trying challenge 5 now..But not able to clear the challenege 5..could you help me?

warehousecalloutservice:
public with sharing class WarehouseCalloutService implements Queueable, Database.AllowsCallouts {  
   private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';    
   public static void runWarehouseEquipmentSync(){
       Http http = new Http();
       HttpRequest request = new HttpRequest();
       request.setMethod('GET');
       request.setEndpoint(WAREHOUSE_URL);
       HttpResponse response = http.send(request);
       if(response.getStatusCode() == 200) {
           List<Object> jsonResponse = (List<Object>)JSON.deserializeUntyped(response.getBody());
           system.debug('~~ '+jsonResponse);
           List<Product2> productList = new List<Product2>();
           for(Object ob : jsonResponse) {
               Map<String,Object> mapJson = (Map<String,Object>)ob;
               Product2 pr = new Product2();
               pr.Replacement_Part__c = (Boolean)mapJson.get('replacement');
               pr.Name = (String)mapJson.get('name');
               pr.Maintenance_Cycle__c = (Integer)mapJson.get('maintenanceperiod');
               pr.Lifespan_Months__c = (Integer)mapJson.get('lifespan');
               pr.Cost__c = (Decimal) mapJson.get('lifespan');
               pr.Warehouse_SKU__c = (String)mapJson.get('sku');
               pr.Current_Inventory__c = (Double) mapJson.get('quantity');
               productList.add(pr);
           }            
           if(productList.size()>0)
               upsert productList;
       }         
   }
   }    

warehousecalloutserviceMock:
@isTest
global class WarehouseCalloutServiceMock implements HttpCalloutMock {
    // implement http mock callout
    global static HttpResponse respond(HttpRequest request){
        System.assertEquals('https://th-superbadge-apex.herokuapp.com/equipment', request.getEndpoint());
 

        System.assertEquals('GET', request.getMethod());

        // Create a fake response
 

        HttpResponse response = new HttpResponse();
 

        response.setHeader('Content-Type', 'application/json');
 

        response.setBody('[{"_id":"55d66226726b611100aaf741","replacement":false,"quantity":5,"name":"Generator 1000 kW","maintenanceperiod":365,"lifespan":120,"cost":5000,"sku":"100003"}]'); 
        response.setStatusCode(200);
        return response;
 
    } 
}

warehousecalloutservicetest
@isTest 
private class WarehouseCalloutServiceTest {    
    @isTest   
  static void testWareHouseCallout(){
        Test.startTest(); 
        // implement mock callout test here
        Test.setMock(HTTPCalloutMock.class, new WarehouseCalloutServiceMock());
    WarehouseCalloutService.runWarehouseEquipmentSync();
        System.enqueueJob(new WarehouseCalloutService());
        Test.stopTest();
        System.assertEquals(1, [SELECT count() FROM Product2]);
    }     
}

User-added image

error : Class WarehouseCalloutService must implement the method: void System.Queueable.execute(System.QueueableContext)
Pankaj  Kumar MauryaPankaj Kumar Maurya

Please refer the below forum for the resolution.

https://developer.salesforce.com/forums/?id=906F0000000fzr3IAA