You need to sign in to do that
Don't have an account?
Peace
Error: Upsert with a field specification requires a concrete SObject type
Hi,
I am new to apex.
I am trying to perform upsert operation using an external id
What changes should i make in below code, so that the following error will be resolved.
Error: "Upsert with a field specification requires a concrete SObject type"
List<SObject> accstoupload; accstoupload = new List<SObject>(); //Code to fetch external_id Field names for the Selected object Schema.SObjectType targetType = Schema.getGlobalDescribe().get(selectedValue); //Selected value is the object name selected from picklist Map<String, Schema.SObjectField> M = targetType.getDescribe().fields.getMap(); for (String k : M.keySet()) { if(M.get(k).getDescribe().isExternalID()) { externalIdFields.add(k); } } // code to upsert for( Integer j = 0; j < externalIdFields.size(); j++) { try{ upsert accstoupload externalIdFields; // Getting error on this line } catch (Exception e) { ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later'); ApexPages.addMessage(errormsg); } }
Please help.
You have to use concrete Sobject after insert,upsert,update,delete.
Here after upsert you wrote
upsert accstoupload externalIdFields;
Here it needs to be
upsert accstoupload;
You can modify your code. First add the field values to the object variable and then upsert the object variable.
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Interesting stuff!
Loved the approach.
Few questions
So instead of doing
I guess you can do something like
So this is how you can do the upsert dynamically .
Please remember you cannot do upsert based on multiple external fields, and hence we have user externalIdFields[0] in code
Hi,
The problem is you are adding the external id value in to the following Sobject list:
List<SObject> accstoupload;
accstoupload = new List<SObject>();
Instead of that create specific object list and add your values in to it.
eg . List<Account> accstoupload = new List<Account>();
Hi Avidev9,
For dynamic upsert operation, i tried your solution but then it again gives similar error, shown below
Code:
Error :
Please help..