Issue I am getting this error while upserting pricebookentry on an external ID
Batch_RDM_PriceBookEntries_Update:execute:errorMessage : The following error has occurred.INVALID_FIELD_FOR_INSERT_UPDATE: Unable to create/update fields: CurrencyIsoCode
- We want to create the pricebookentries when they are not present and update them when they are available
- Inserting is working fine, however, while updating I get this error
- I am aware that I cannot modify Currency, Product and Pricebook Ids however, I am not trying to do that, I just want to update the cost and price when we update.
- We cannot delete the existing pricebookentry and create a new one, as the pricebookentries are used in other objects and deleting pricebookentries will break multiple things
What am I doing wrong, any help would be appreciated
Code
for(RDM_Price_Book_Entry__c RDMPbe:scope) {
PricebookEntry pbe = new PricebookEntry();
pbe.Product2 = new Product2(RDM_Id__c = RDMPbe.Product_External_Id__c);
pbe.Pricebook2Id = RDMPbe.Unit_of_Measure__c=='Days'?PBDailyRate:PBHourlyRate;
pbe.IsRDMReference__c = true;
pbe.Cost__c = RDMPbe.Standard_Cost__c;
pbe.CurrencyIsoCode = RDMPbe.CurrencyIsoCode;
pbe.RDM_Id__c = RDMPbe.Unit_of_Measure__c=='Days'? //External Id
PBDailyRate+RDMPbe.Product_External_Id__c+RDMPbe.Unit_of_Measure__c+RDMPbe.CurrencyIsoCode:
PBHourlyRate+RDMPbe.Product_External_Id__c+RDMPbe.Unit_of_Measure__c+RDMPbe.CurrencyIsoCode;
pbe.Last_Update__c = operationId;
pbe.IsActive = true;
pbe.UnitPrice = RDMPbe.Target_Price__c;
system.debug('Batch_RDM_PriceBookEntries_Update:execute:PricebookEntry RDM ID'+pbe.RDM_Id__c);
PBEntryToUpsert.add(pbe);
}
try{
Database.UpsertResult[] upsertResult = Database.upsert(PBEntryToUpsert, PricebookEntry.RDM_Id__c, False);
for (Database.UpsertResult saveResult : upsertResult) {
if (!saveResult.isSuccess()) {
// Operation failed, so get all errors
for(Database.Error error : saveResult.getErrors()) {
errorEncountered = true;
errorMessage+= 'The following error has occurred.';
errorMessage+= error.getStatusCode() + ': ' + error.getMessage();
system.debug('Batch_RDM_PriceBookEntries_Update:execute:errorMessage : '+ errorMessage);
}
}
}
} catch (Exception ex) {
errorEncountered = true;
errorMessage = 'Batch_RDM_PriceBookEntries_Update : '+ex+' : ' +ex.getStackTraceString();
throw new CustomException('## debug Batch_RDM_PriceBookEntries_Update.execute : ' + errorMessage);
}
}
Issue