I copied the product data to another object named foo. I used only the name and externalId of product to copy it to Foo object. Foo's external id matches with product's external Id.
Some of my product's Name have changed. So I want to run a script to update corresponding foo records name. Since the external Id is same between product and foo , I can use that as a reference to upsert the records in Foo. Below is my code, however I get this error:
System.DmlException: Upsert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: Externald__c duplicates value on record with id: a1H370000016QkV: []
Apex code:
List<Product2> products = [SELECT Name,PrdExternalId,Product__r.PrdExternalId FROM Product2 WHERE Warehouse_SKU__c!=Null];
system.debug('Product list ********* ' + products);
List<Foo__c> flist = new List<foo__c>();
for(Product2 p : products){
foo__c f = new foo__C();
f.Externald__c = p.PrdExternalId;
f.Name = p.Name;
f.foo__r.Externald__c = p.Product__r.PrdExternalId; ->> NULL POINTER EXCEPTION HERE
flist.add(f);
}
upsert flist;
Part 2- When I try to use f.foo__r.Externald__c = p.Product__r.PrdExternalId;
to reference the self lookup on foo__c object i get
System.NullPointerException: Attempt to de-reference a null object