Javascript Remoting – sending sObject as argument

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.

Answer

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
Source : Link , Question Author : Matt Baker , Answer Author : Alex Tennant

Leave a Comment