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
TehNrdTehNrd 

SObject JS Remoting Error - Please provide an 'id' or 'sobjectType' value.

The salesforce.com docs states remoting methods can take the following values as arugments, "primitives, collections, typed and generic sObjects, and user-defined Apex classes...". There appears to be an issue with generic sObject. Take a look at the following Visualforce page and the related remoting method.

 

<apex:page showHeader="true" sidebar="true" controller="RemotingSObject">
	<input value="Click Me" onclick="doRemote();" type="button"/>

	<script type="text/javascript">
		
		function doRemote(){
			var obj = {
				sobjectType: 'Account',
				Name: 'My new account'
			};

			//You can see there is a value of sbojectType set
			console.log(obj);

			Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemotingSObject.receiveSObject}', obj, function(result, event){
		        console.log(event);
		        console.log(result);
			});
		}

	</script>
</apex:page>

 The Class:

public with sharing class RemotingSObject {
	@RemoteAction
	public static void receiveSObject(SObject obj) {
		system.debug(obj); 
	}
}

 If you look in the browsers JavaScript console you will see this error message:

Visualforce Remoting Exception: Unable to determine SObject type: SObject. Please provide an 'id' or 'sobjectType' value.

 

Makes no sense as an sobjectType value is being supplied. Oddly enough if you provide an id value it works...but if you are trying to insert a new sObject you do not yet have an id value to use.

 

Any ideas on how to make this work? Bug?

 

Thanks,

Jason

 

Best Answer chosen by Admin (Salesforce Developers) 
cwall_sfdccwall_sfdc

Roger that.  We will fix in the next patch.  Stay tuned.

All Answers

cwall_sfdccwall_sfdc

Roger that.  We will fix in the next patch.  Stay tuned.

This was selected as the best answer
TehNrdTehNrd

Cool, thanks.

 

Case # for this was 08708634 if you want to sync up internally.

SFDevelopersSFDevelopers

Alternative solution thats working fine for me :

 

<apex:page showHeader="true" sidebar="true" controller="RemotingSObject">
    <input value="Click Me" onclick="doRemote();" type="button"/>

    <script type="text/javascript">
		
	function doRemote(){
    var obj = '{"attributes":{"type":"Account"},"Name":"11111"}'; //You can see there is a value of sbojectType set     console.log(obj); Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemotingSObject.receiveSObject}',
obj, function(result, event){
                 console.log(event); console.log(result); }); } </script> </apex:page>
public with sharing class RemotingSObject {
   @RemoteAction
   public static void receiveSObject(String param) {
Account obj = (Account) Json.deserialize(param, Account.class); system.debug(obj); } }

 

Here am passing String as parametes in Remote Action Call & using Json deserialize method to get Account object.

 

This also works for List<SObject>.

TehNrdTehNrd

Unfortunately this isn't really a solution as it requires you to hard code the cast of the string to an Account object type, (Account). 

 

Hardcoding object types defeats the purpose of working with sObjects.

SFDevelopersSFDevelopers

Yes, we have to hard code the type.

 

But if you have to perform the DML statement then we can hardcode List<SObject> And can perform DML statement.

Am also work on an app http://screencast.com/t/m8zHrXNnH which also have the same requirement.

 

 

public with sharing class RemotingSObject {
   @RemoteAction
   public static void receiveSObject(String param) {
List<SObject> objList = (Account) Json.deserialize(param, List<SObject>.class);
system.debug(objList);
insert objList; } }

 

Kanika Anand 8Kanika Anand 8
Was this issue resolved?
Pravin Bhujbal 8Pravin Bhujbal 8
No it does not seem to be resolved yet, since we are still getting the same error. We get this error when using both the generic Object and SObject types. 
@cwall_sfdc, can you please confirm whether it was resolved and patched?
sudarshana deysudarshana dey
Please let me know if this issue is resolved.
Peter-NogesPeter-Noges
This is probably fixed.  I tested the original post's code in a developer org and saw no issues in Debug logs or browser console.