Upserts with Generic sObjects – Salesforce

467 Views

Have you ever run into this error? “Upsert with a field specification requires a concrete SObject type”

Basically it is saying that you cannot declare a list of type sObject and then try to use the upsert command against that list. That limitation can make coding generic routines difficult which do not know the specific object type up front. For example, I might be building a table driven integration layer where the user will determine, by updating a custom field or custom setting, what the name of the destination custom object will be. With this approach I will need to use the generic sObject data type, but that will stop me from using the upsert database command.

Bummer! What to do?

Here is a link that describes this problem very clearly and jangiz at the bottom describes the correct work-around approach.

Here is some code to implement that work-around that jangiz described:

Wow, what a bunch of work when I really just wanted to write…
upsert sObjList myExternalIDField

But as long as there is a manageable work-around I can at least move forward!

Good luck with your Apex today!

Source : https://www.sundoginteractive.com/blog/generic-sobjects-salesforce

Leave a Comment