This question appears to have been answered several times on here, but none of the solutions are working for me. I have an Apex function defined like so:
@RemoteAction
global static ActivityWrapper createActivity(Activity__c activity)
And when I call this Javascript function:
Controller.createActivity(
{ForWeek__c: "2014-04-21T08:00:00.000Z",
Notes__c: "sdfsdf",
Type__c: "Referral",
Id: null,
sObjectType: "Activity__c"}, callback);
I always get back this error message:
Visualforce Remoting Exception: Unexpected type for Controller.createActivity(Activity__c)
As far as I know this is all that is required, but I can't get past this.
Attribution to: Matt Baker
Possible Suggestion/Solution #1
EDIT 2:
I've also just noticed that you're passing a formatted date string, with Visualforce Remoting you need to pass your Date values as Unix timestamp values (in milliseconds, rather than seconds).
So for your example you need to pass 1398067200000
.
EDIT:
In fact, I've just noticed that your JSON is poorly formed, you are missing a few commas (after the ForWeek__c
, Notes__c
and Type__c
values). Try this:
Controller.createActivity
(
{
ForWeek__c: "1398067200000",
Notes__c: "sdfsdf",
Type__c: "Referral",
Id: null
},
callback
);
I've tried replicating your issue and I've not managed to end up with exactly the same error message, however I have stripped it down to the simplest example.
The only solution I can offer you is to try and pull your controller and page back to it's simplest possible version (i.e. something that looks like the below) and build from there, because other than setting the sObjectType
field I can't see anything wrong with what you're doing.
RemotingSObject__c
- Field__c (String)
RemotingSObjectController
public class RemotingSObjectController
{
@RemoteAction
public static RemotingSObject__c getRemoteObject()
{
RemotingSObject__c anObject = new RemotingSObject__c(Field__c = 'Value');
return anObject;
}
@RemoteAction
public static String setRemoteObject(RemotingSObject__c anObject)
{
return anObject.Field__c;
}
}
RemoteObjectPage
<apex:page controller="RemotingSObjectController">
<script type="text/javascript">
var sobject;
function getRemoteObject()
{
RemotingSObjectController.getRemoteObject
(
function(result, event)
{
if (event.status)
{
console.log(result);
}
else if (event.type === 'exception')
{
console.log(event.message);
}
else
{
console.log(event.message);
}
}
);
}
function setRemoteObject()
{
RemotingSObjectController.setRemoteObject
(
{Field__c: 'Success!'},
function(result, event)
{
if (event.status)
{
console.log(result);
}
else if (event.type === 'exception')
{
console.log(event.message);
}
else
{
console.log(event.message);
}
}
);
}
</script>
<input type="button" onclick="getRemoteObject();" value="get"/>
<input type="button" onclick="setRemoteObject();" value="set"/>
</apex:page>
Result
Clicking the get
button results in a JSON representation of my RemotingSObject__c
in my Javascript log with 'Value'
in Field__c
, clicking the set
button results in 'Success!'
being printed to my Javascript log.
Attribution to: Alex Tennant
Possible Suggestion/Solution #2
While Alex's response hits the root cause of my original question, I'm glad this confusion arose. Do not do what I did accessing the window scoped Controller helper unless you want to reinvent the wheel. If you need to use Javascript Remoting for non-CRUD purposes use the "official" method raised by @sfdcfox
{!$RemoteAction.remote.createAccount}({ Id: null, Name: "Nobody", Industry: "Technology" }, function(d,e) { console.log(d.Id); });
If Remoting is only a means to get at CRUD (like my case) I recommend using the RemoteTK component + controller available here:
https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit/blob/master/RemoteTK.component
As a huge plus, this also allows you infinite CRUD without worrying about API limits. It should really not be hidden alongside the forcetk
library though. I completely overlooked it when I originally pulled in ForceTK.
Attribution to: Matt Baker
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33270