I am trying to return a map of object to sobject. However I get a parsing error:

VFRemote.js:117 Visualforce Remoting Exception: Error parsing json response:

'Unexpected token {'. Logged in?

I have no issues returning a map. That's what I find bizarre.

Has anyone returned a map of 2 objects before? Is the problem in my code?


Here's my code:

Map<Contact, sObject__c> testMap = new Map<Contact, sObject__c>();
testMap.put(newContact, newsObject);
return testMap;

newContact and newsObject are created by me and inserted by me in the database. So on successful insert I populate the testMap with their values and try to return them.

  • can you please post the code to return the map? You have posted the error which doesn't help anyone trying to answer.
    – abhi
    Apr 5, 2016 at 19:41
  • @abhi - okay there.
    – Arthlete
    Apr 5, 2016 at 19:45
  • 1
    One of the possible explanations may be next: when you try to return Map<sObject, sObject> in vf remoting, SF trying to convert that to JS object, with key and value, where key is object, and value is object as well. In case of value, that is being ok, but in case of key -- in SF side, when you call JSON.serialize(sObject), that returns string like "attributes":{"type":"Contact","url":"/services/data/v36.0..."},"Id":"003E00000196fNEIAY","Name":"John Dodge"}, and in general it would be { {...}:Object, ... }. salesforce "forgets" to encode key - and that's why error appears.
    – kurunve
    Apr 5, 2016 at 20:22
  • Can you show us the JS part of the same call? Apr 5, 2016 at 20:49
  • @SebastianKessel I am able to reproduce the same issue as author wia this JS function remoteCall(callback){ Visualforce.remoting.Manager.invokeAction( '{!$RemoteAction.VFRemoteExample.testRequest}', 'param', callback, {escape: true} ); }; remoteCall([], function(res,r){ console.log(res); });. That is SF issue, but they don't recommend to use sObjects as map keys in apex.
    – kurunve
    Apr 5, 2016 at 21:18


You must log in to answer this question.

Browse other questions tagged .