The upsert()
DML operation requires a specific List based on a specific sObject type, for instance:List<Account> lstAccount = new List<Account>();
However, we are trying to generate a generic method which allows us to pass by an sObject list containing all data records to be upserted for a specific sObject. An insert()
operation allows us to use a List<sObject>
to be inserted but the upsert()
function is rejecting it:
List<sObject> listWRecords = new List<sObject>();
listWRecords.add(new Foo__c(Name="Record1"));
listWRecords.add(new Foo__c(Name="Record2"));
upsert(listWRecords, 'ExternalIdFieldFrom_Foo__c', false);
Which will result in an error saying that the upsert must be performed on a concrete entity type.
Dynamically casting an sObject list is also not possible; I've seen solutions to 'hard code' or create Wrapper classes for each sObject on your org. But we would like to create a generic option here.
Another possibility would be to create some logic mimicking an upsert by splitting it into an update()
and an insert()
operation. Keeping in mind Salesforce limits, I do not see an option to go this way.
Best Answer
I know this approach is strange because you are still working with a
List<SObject>
, but when you assign it you can make it more specific (e.g.List<Account>
) by usingType.forName
andType.newInstance
methods.The
getSObjectType
call may not be completely reliable, especially since some of these records are being inserted and hence won't have ids. For that reason, it is probably better to acceptsObjectType
as an additional parameter instead of trying to determine it on the fly.Update
Some bad news, as discovered here (actually earlier than your post), the above methodology does not always work. Specifically, if I want to perform a partial
upsert
and also specify an external Id field, I get a compile fail.Additional Update
I have a case open with support and it has been escalated to Tier 3. Currently the responses I'm getting are all along the lines of "will update you tomorrow," but I will post here if I get any resolution.
Additional Update
Support claims the three parameter signature failure is WAD. They said if I want them to actually fix it, I need to post on an Idea, so here it is, vote for it!