function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pooja GargPooja Garg 

Error: Upsert with a field specification requires a concrete SObject type with external ID

Hi 

 

   I have implemented Inserting data to an SObject dynamically..... now I need to make it UPSERT functionality with given External ID field but it is throwing error as Upsert with a field specification requires a concrete SObject

 


Here is my Code : 

@RestResource(urlMapping='/JSONTest/*')
global with sharing class JSONtest {
@HttpPost
	global static String upsertRowDATA() {
		RestRequest req = RestContext.request;
		String datasetId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
		String JSONString = req.requestBody.toString();
		//Schema.SObjectType custObject = Schema.getGlobalDescribe().get(datasetId+'__c');
		
		List<Object> listJsonObjects = (List<Object>)JSON.deserializeUntyped(JSONString);
		
		Schema.SObjectType soTypeObject = Schema.getGlobalDescribe().get(datasetId + '__c');
		Schema.DescribeSObjectResult objDescription = soTypeObject.getDescribe();
  		Map<String, schema.Sobjectfield> fieldsMap = objDescription.Fields.getMap();
  		system.debug(fieldsMap);
  		for(Object jsonObj : listJsonObjects) {
  			Map<String, Object> mapObj = (Map<String, Object>)jsonObj;
  			
  			system.debug(mapObj);
  			SObject sObj = Schema.getGlobalDescribe().get(datasetId + '__c').newSObject();
			
			for(String fieldName : fieldsMap.keySet()) {
			
				if(fieldName.indexOf('__c') != -1) {
					Object fieldData = mapObj.get(fieldsMap.get(fieldName).getDescribe().getName());
		   			sObj.put(fieldName, fieldData);
				}
	    	}
	    	upsert sObj ;
  		}
		 
		return JSON.serialize('{success : true}');
    }
}

 here "datasetId + '__c' " is my string type to get SObject reference.....!!!

 

now here in code if I write upsert sObj externalId__c;  then it gives me error that field is not a concreate SObject type....!!!

 

 

Could anyone suggest me the solution as per my requirement INSERT is working perfectly but getting problem for Updating...................... !!!

 

 

Thanks in Advance...!!

Pooja :)

 
crop1645crop1645

Pooja

 

Your upsert statment is upserting an object of type SObject

upsert sObj 

 You need to cast this back to an Account, Case, whatever before you can do the upsert. You can use SObject method getSObjectType() to find the concrete SObject for the cast.

 

see also: http://boards.developerforce.com/t5/Apex-Code-Development/Upsert-List-of-Generic-sObjects/td-p/196329/page/2