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
Dominik Ryczek 5Dominik 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:
@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 :)
 
Narender Singh(Nads)Narender Singh(Nads)

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!

Dominik Ryczek 5Dominik Ryczek 5
Hi!
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?
Narender Singh(Nads)Narender Singh(Nads)
Hi Dominic,
Is this problem not letting you complete your challenge?
Dominik Ryczek 5Dominik Ryczek 5
Good question :)
It turned out that I can complete this challenge with this issue. But it's little weird.
Thanks!
Narender Singh(Nads)Narender Singh(Nads)
Hi,
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 :)
Dominik Ryczek 5Dominik Ryczek 5
Yes, I know it. But shouldn't external ID be unique in this case? Without checking 'Unique' there was no exception but when I was calling runWarehouseEquipmentSync() method the existing records were duplicated instead of updated. Why? I don't think it should behave like this :)
Anyway challenge is completed, thank you for help.
Narender Singh(Nads)Narender Singh(Nads)
I think you might have missed something while declaring the fields because my code is running perfectly.
And you're welcome. :)
Urvashi BabbarUrvashi Babbar
After being stuck on this challenge for so long I finally got my solution:

Please check answer at: 
https://developer.salesforce.com/forums/?id=9060G0000005YMlQAM