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
banderson5144Devbanderson5144Dev 

Visualforce Remoting Javascript sObject

Can someone please provide me with Sample Code to create a Javascript Sobject for VF remoting? Here's what I have so far and it doesnt work:

 

Page:

<apex:page showHeader="false" standardStylesheets="false" sidebar="false" doctype="html-5.0" controller="testVFRemote">
    
    
	<input type="button" onclick="myFunc();" value="Click Me"/>
	
	<script type="text/javascript">
		
		function myFunc()
		{			
			var vfRemoteVar = {sobjectType:"Account",Name:"My New Account"};
						
			Visualforce.remoting.Manager.invokeAction(
				'{!$RemoteAction.testVFRemote.insAcct}',
				vfRemoteVar,
				function(result, event){
					alert(result);
					alert(event);
				}, 
				{escape: true}
			);
		}
	</script>
</apex:page>

 Controller:

public with sharing class testVFRemote
{
	@RemoteAction
    public static Database.Saveresult insAcct(sObject sobj)
    {
    	return Database.insert(sobj);
    }
}

 heres the error i am getting:

 

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

banderson5144Devbanderson5144Dev

Anyone?

banderson5144Devbanderson5144Dev

Come on I know i am not the only one who must have asked this question before.

JitendraJitendra

Try this code for controller

 

public with sharing class testVFRemote
{
	@RemoteAction
    public static Database.Saveresult insAcct(Account sobj)
    {
    	return Database.insert(sobj);
    }
}

 

banderson5144Devbanderson5144Dev

I just get this now for the error:

 

Visualforce Remoting Exception:

JitendraJitendra

Please, provide complete error stack so that we can help you.

banderson5144Devbanderson5144Dev

Request:

{
	"action" : "testVFRemote",
	"method" : "insAcct",
	"data" : [{
			"sobjectType" : "Account",
			"Name" : "My New Account"
		}
	],
	"type" : "rpc",
	"tid" : 3,
	"ctx" : {
		"csrf" : "9WezqfOM0gjO.qZc2ZNsmvBLu7s7hdSges6TOLxMZQkr4R0STtanGxGEGnWUhPo7NV_G.EWjRlclfESnNqYTd2pLXwQQp_AzUBIF5l5SusM0QfyEzs49jG8MiRYi6JOjxyZH_pYWf5Bp1wGohGVO6CsYtWk=",
		"vid" : "066d0000001VKjS",
		"ns" : "",
		"ver" : 25
	}
}

 

Response:

 

[{
		"type" : "exception",
		"tid" : 3,
		"action" : "testVFRemote",
		"method" : "insAcct",
		"message" : "",
		"where" : "",
		"data" : [{
				"Name" : "My New Account",
				"sobjectType" : "Account"
			}
		],
		"vfTx" : true,
		"vfDbg" : true
	}
]

 

JitendraJitendra

This is realy strange. I will try to reproduce at my side. Is there any required field on your Account object, which we are missing?

banderson5144Devbanderson5144Dev

Nope. Just a basic dev account. I have used VF remoting to pass in strings and other data types. But i wanted to try passing in sObjects.

banderson5144Devbanderson5144Dev

So I have partially solved my problem. Instead of using a Generic sObject type, I am defining a concrete sObject type of Account. I am also leveraging the AJAX toolkit (don't know if its absolutely necessary, so someone can tell me otherwise) to create the Account sObject variable in Javascript for me. Here is my code and the data being sent back and forth

 

Controller:

public with sharing class testVFRemote
{
	@RemoteAction
    public static Database.Saveresult insAcct(Account sobj)
    {
    	return Database.insert(sobj);
    }
}

 VF Page:

<apex:page showHeader="false" standardStylesheets="false" sidebar="false" doctype="html-5.0" controller="testVFRemote">
    
    <script type="text/javascript">
		var __sfdcSessionId = '{!GETSESSIONID()}';
	</script>
	<script src="../../soap/ajax/25.0/connection.js" type="text/javascript"></script>
	
	<input type="button" onclick="myFunc();" value="Click Me"/>
	
	<script type="text/javascript">
		
		function myFunc()
		{			
			var vfRemoteVar = new sforce.SObject("Account");
			vfRemoteVar.Name = "My New Account";
			
			var newObj = eval("("+vfRemoteVar.toString()+")");
			
			delete newObj.type; //Don't need the type property from AJAX toolkit to be sent
						
			Visualforce.remoting.Manager.invokeAction(
				'{!$RemoteAction.testVFRemote.insAcct}',
				newObj,
				function(result, event){
					alert(result);
					alert(event);
				}, 
				{escape: true}
			);
		}
	</script>
</apex:page>

 

Request Data:

{
	"action" : "testVFRemote",
	"method" : "insAcct",
	"data" : [{
			"Name" : "My New Account"
		}
	],
	"type" : "rpc",
	"tid" : 2,
	"ctx" : {
		"csrf" : "PUNu63BGtTFtPFhr_JGw8VDeX_xSvbMGs5vG8sLQhIIsYDbOTbUTsB0AclmhSXXCbf.Mdj_nAGBl2kQGe_JUMc9XqfZUZ5KaHoNx18N1X.DSNR3fo0DMbDfmG1oirrb9OLdWUqjhooAvIzo1D8tk6uAxlbI=",
		"vid" : "066d0000001VKjS",
		"ns" : "",
		"ver" : 25
	}
}

 

Response Data:

[{
		"type" : "rpc",
		"tid" : 2,
		"action" : "testVFRemote",
		"method" : "insAcct",
		"result" : {
			"success" : true,
			"id" : "001d000000HS8wAAAT"
		}
	}
]

 Although this does solve my immediate problem, I would still like to get the generic sObject function working so I am not marking this solved yet.