You need to sign in to do that
Don't have an account?
Rajeevan 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.
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 Vakati