+ Start a Discussion
PeacePeace 

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.

souvik9086souvik9086

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

Avidev9Avidev9

Interesting stuff!

Loved the approach.


Few questions

  • Why are you doing upsert again and again based on different external Id ?
  • I guess if its a utility class you should pass the external Id while doing upsert, isnt it ? Else you will end up creating new records
  • I guess this code should work fine if you have one external Id in the object.

So instead of doing

upsert accstoupload externalIdFields;

I guess you can do something like

 

database.upsert(accstoupload,false,externalIdFields[0]);

 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

 

CoolSurenCoolSuren

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>();

PeacePeace

Hi Avidev9,

 

For dynamic upsert operation, i tried your solution but then it again gives similar error, shown below

 

Code:

database.upsert(accstoupload,selectedValue.externalIdFields[0],false);  
//selectedValue is the object name selected from picklist

 Error :

Initial term of field expression must be a concrete SObject

 Please help..