You need to sign in to do that
Don't have an account?
Dominik Ryczek 5
Apex Specialist Superbadge- step 2
Hi,
I have a problem with step 2 in Apex Specialist Superbadge. I don't know how I should use field Warehouse_SKU__C as an external ID. This is my method from WarehouseCalloutService class:
I couldn't find how to use external ID field instead of using standard ID during upserting.
Thank you in advance :)
I have a problem with step 2 in Apex Specialist Superbadge. I don't know how I should use field Warehouse_SKU__C as an external ID. This is my method from WarehouseCalloutService class:
@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); if(response.getStatusCode() == 200) { List<Product2> equipmentToSF = new List<Product2>(); List<WarehouseResponse> equipment = WarehouseResponse.parse(response.getBody()); for(WarehouseResponse itemFromWH : equipment) { Product2 item = new Product2(); item.Warehouse_SKU__c = itemFromWH.sku; item.Name = itemFromWH.name; item.Maintenance_Cycle__c = itemFromWH.maintenancePeriod; item.Lifespan_Months__c = itemFromWH.lifespan; item.Cost__c = itemFromWH.cost; item.Current_Inventory__c = itemFromWH.quantity; item.Replacement_Part__c = true; equipmentToSF.add(item); } upsert equipmentToSF; } }The problem is that every time I call this method, the equipment from external DB is duplicated instead of updated. I try to set Warehouse_SKU__c field as unique but it causes error.
I couldn't find how to use external ID field instead of using standard ID during upserting.
Thank you in advance :)
Hi Dominik,
You have to set the Warehouse_SKU__c field as External ID. To do this all you have to do is check the checbox for 'External ID' from the UI for the field. This should solve your purpose.
Let me know if i can help you in any other way. :)
Mark this answer as the best answer if it solves your problem.
Thanks!
I have this checkbox checked and it is still the same problem. I also checked 'Unique' checkbox to stop duplicate record while testing :)
Now I have this exception:
System.DmlException: Upsert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: Warehouse_SKU__c duplicates value on record with id: 01t1r000006YDSm: []
Do you have any ideas?
Is this problem not letting you complete your challenge?
It turned out that I can complete this challenge with this issue. But it's little weird.
Thanks!
I believe the reason you are getting the DMLException is you have that field checked for unique, only the externalID checkbox needs to be checked for smooth operation.
Anyway, congratulations :)
Anyway challenge is completed, thank you for help.
And you're welcome. :)
Please check answer at:
https://developer.salesforce.com/forums/?id=9060G0000005YMlQAM