I'm having this dilemma regarding the result for Database.Upsert(List, boolean).
Database.Upsert returns Database.UpsertResult and it provides as the Id of the record that was successfully inserted/updated.
However, if an ERROR happens during Insert, when you try to get the Id in UpsertResult via getId() function, you will see that the Id is NULL.
Now I understand this part because the record does not yet exist in DB but I need to return the UpsertResult to another method that needs to know which record failed.
Ex:
List<Asset> finalAssetList = new List<Asset>();
//Loop exists here that fills up finalAssetList
List<Database.UpsertResult> upsertAssetResult = Database.upsert(finalAssetList, false);
List<ResultMessage> rmList = new List<ResultMessage>();
for(Database.UpsertResult ur : upsertAssetResult) {
ResultMessage rm = new ResultMessage();
rm.error_message = ur.getErrors();
rm.recordId = ur.getId();
rmList.add(rm);
}
return rmList;
Please take note that ResultMessage is an inner class whose variables are recordId and error_message.
If an Error happens during Insert, my rmList will contain a record with NULL in its recordId.
Is there any way that I can circumvent this?
Thanks.