15

Currently I have a controller similar to the following (used example, it's easier to understand):

global class myClass
{
    global class wrapperClass
    {
        public String name {get; set;}
        public Account account {get; set;}

        public wrapperClass()
        {
            account = new Account();
        }
    }

    @RemoteAction
    global static wrapperClass executeProcess(wrapperClassArg arg1)
    {
        reutrn wrapperClassArg;
    }
}

Then from the VF page if I pass an empty JS object for the wrapper class parameter, it works good and I can set the primitive types of variables in the wrapper class:

var wrapper = new Object();
wrapper['name'] = 'Test';

myClass.executeProcess(wrapper, function(result, event)
{
    if(event.type === 'exception')
    {
        console.log(event);
        return false;
    }
    else if(result)
    {
        console.log(result);
        // do something
    }
    else
    {
        console.log(event);
    }
});

which results with object and the name variable is set without problems.

Now the interesting bit... How can you set an sObject from the VF page?

I've done this in the past for single public sObjects, but doesn't seem to work if the sObject is inside a wrapper class. So I've tried the following:

var wrapper = new Object();
wrapper['name'] = 'Test';
wrapper['account'] = new Object();
wrapper['account']['Name'] = 'Test';

myClass.executeProcess(wrapper, function(result, event)
...

which results with the following error:

Unexpected type for myClass.executeProcess(myClass.wrapper)

EDIT: I have also tried setting sobjectType = 'Account' and Id = null / '' for the javascript account object.

Does anyone know a workaround for the above issue?

2
  • Note working for me as well. I am returning string json from page and want to deserialize that in remoteaction method. below is the code i am using List<Wrapper> lstSelectedFlightLines = (List<Wrapper>)JSON.deserialize(jsonString, List<Wrapper>.class); error: Visualforce Remoting Exception: Unexpected character ('p' (code 112)): was expecting double-quote to start field name at [line:1, column:4] Please suggest!
    – user3330
    Jul 18, 2013 at 17:10
  • did you ever figure this out, beyond just sending raw JSON strings? I have also tried setting both sobjectType and Id but it always tells me that it received an unexpected type.
    – Matt Baker
    Apr 21, 2014 at 19:07

3 Answers 3

5
@RemoteAction
    global static wrapperClass executeProcess(string arg1)
    {
        JSONParser parser = JSON.createParser(arg1);
        wrapperClass wrap = (wrapperClass)parser.readValueAs(wrapperClass.class);
        return wrap;
    }
7
  • 1
    The argument is a wrapperClass type, not a string... Mar 4, 2013 at 23:05
  • Even if I change it to string, I'm getting: "Unexpected character ('p' (code 112)): was expecting double-quote to start field name at [line:1, column:3]" Mar 4, 2013 at 23:49
  • To convert an object to a string, use JSON.stringify: Mar 5, 2013 at 0:14
  • 1
    Did you try this code if it's working or not? I doubt as you have a typo at reutrn Mar 5, 2013 at 0:20
  • yes i working in this way. myClass.executeProcess(JSON.stringify(wrapper)......; Mar 5, 2013 at 0:24
2

Have you tried defining a javascript object that matches the SObject you want? Maybe even for your wrapper class you should do the same.

For example:

// in Javascript
function WrapperClass()
{
    this.Name = '';
    this.Account = new Account();
}

function Account()
{
    this.Id = null;
    this.Name = '';
}

// in your code:
var wrapper = new Object();
wrapper['name'] = 'Test';

I suspect it's either that you're not using a matching object definition (unlikely) or that you're not including an ID field at all.

Wes Nolte has written what I regard to be the best post on remoting here.

1

This appears to work for me...

var wrapper = new Object();
wrapper['name'] = 'Test';
wrapper['account'] = new Object();
wrapper['account']['Name'] = 'My Account';
wrapper['account']['AccountNumber'] = '123456789';

So that in the 'executeProcess' method the following

System.debug(arg1.account.AccountNumber);

Results in this...

14:47:05.055 (55236000)|USER_DEBUG|[19]|DEBUG|123456789

I am using the latest platform API in my meta data files, all other aspects of my code is as per your answer.

NOTE: That if you used the SObject data type in your Apex wrapper, according to the documetnation you need to define also 'sobjectType' at the client end, giving it the value 'Account'. As per the docs here. However since your wrapper explicitly uses the Account data type, the JSON deserialiser knowns the type.

Here is the full code.

3
  • Still not working, using v27 API and tried setting the sobjectType as well, no luck... Can you please paste the complete code? Mar 4, 2013 at 23:17
  • Yes I updated my answer. This should work without having to manually pass JSON and de-serialise it. Mar 5, 2013 at 19:53
  • I tried it again, still didn't work for me. Weird... Mar 5, 2013 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.