+ Start a Discussion
Rajeevan SRajeevan S 

System.ListException: Before Insert or Upsert list must not have two identically equal elements. Error on doing Apex Spealist Superbadge.

Here is the Code used:

Http http = new Http();
HttpRequest requests = new HttpRequest();
requests.setEndpoint('https://th-superbadge-apex.herokuapp.com/equipment');
requests.setMethod('GET');
HttpResponse response = http.send(requests);
List<Product2> prodList = new List<Product2>();        
if(response.getStatusCode() == 200){
           Product2 prod = new Product2();
           List<Object> equipments =(List<Object>) JSON.deserializeUntyped(response.getBody()) ;
         Set<String> skuSet=new Set<String>();    
        for(Object o: equipments){            
            Map<String,Object> prodMap = (Map<String,Object>)o; 
            //System.debug(prodMap);               
            prod.Name = (String)prodMap.get('name');
               prod.Replacement_Part__c = true;
               prod.Cost__c = (Integer) prodMap.get('cost');
               prod.Current_Inventory__c = (double)prodMap.get('quality');
               prod.Lifespan_Months__c = (Integer)prodMap.get('lifespan');
               prod.Maintenance_Cycle__c = (double) prodMap.get('maintenanceperiod');
               prod.Warehouse_SKU__c = (String) prodMap.get('sku');
               prodList.add(prod);            
            system.debug('Prod:'+prodList);                
       }            
}
upsert prodList Warehouse_SKU__c;



This is the Error im getting:

System.ListException: Before Insert or Upsert list must not have two identically equal elements.

Here is the JSON format link: https://th-superbadge-apex.herokuapp.com/equipment

Please help me to go forward with the challenge.
Raj VakatiRaj Vakati
public with sharing class WarehouseCalloutService {

    private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
    private static final String GET = 'GET';
    
    // complete this method to make the callout (using @future) to the
    // REST endpoint and update equipment on hand.
    @future (callout=true)
    public static void runWarehouseEquipmentSync(){
        List<Product2> upsertList = new List<Product2>();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(WAREHOUSE_URL);
        req.setMethod(GET);
        Http http = new Http();
        HTTPResponse res = http.send(req);
        
        String JSONContent = res.getBody().replaceAll('_id', 'equipmentId');
        JSONParser parser = JSON.createParser(JSONContent);
        
        while (parser.nextValue() == JSONToken.START_ARRAY) {
            if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
                while (parser.nextToken() != null) {
                    if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
                        Equipment inv = (Equipment)parser.readValueAs(Equipment.class);
                        upsertList.add(new Product2(Lifespan_Months__c = inv.lifespan, 
                                                    Cost__c = inv.cost,
                                                    Replacement_Part__c = true,
                                                    Maintenance_Cycle__c = inv.maintenancePeriod,
                                                    Warehouse_SKU__c = inv.sku,
                                                    Current_Inventory__c = inv.quantity,
                                                    Name = inv.name));
                        parser.skipChildren();
                    }
                }
            }
        }
        upsert upsertList Warehouse_SKU__c;
    }
    
    public class Equipment {
        public String equipmentId;
        public Boolean replacement;
        public Integer quantity;
        public String name;
        public Integer maintenancePeriod;
        public Integer lifespan;
        public Integer cost;
        public String sku;    
    }
}