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
Lovel PanchalLovel Panchal 

I am not able to clear the test even when all is working fine (Apex Specialist > 2.Synchronize Salesforce data with an external system)

I am facing issues to clear the mentioned test, Please can some one look into this and help me out.
Help appericiated.
Challenge


Data of Product(Equipment)Code which ran successfully

The Code which I am using:

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;
        }
        
    }
}
Jeff DouglasJeff Douglas
Lovel,

How many records do you have in the Product2 table. There are 22 records that should by imported from the remote endpoint. Therefore, you should have >= 22 records.

Jeff Douglas
Trailhead Developer Advocate
Lovel PanchalLovel Panchal
I have imported records many time now I have lots of records (all duplicate) which are fetched from api for the count they are more than 500 now. All is working fine and as per the process then also it's showing the error
Martijn SchwarzerMartijn Schwarzer
Hi Lovel,

The challenge states that you need to update the equipments. In your code you are adding the equipments from the webservice results, since there is no Salesforce ID known.

You will need to add the external id field to your upsert statement to make sure that the equipments are updated and not added. Like this:
 
upsert LstProduct Warehouse_SKU__c;

Happy coding!

Best regards,
Martijn Schwärzer