2

I'm trying to pass collection to overrided SObjectModel update function. If I pass primitives, I can get them in APEX. But if I pass nested collection, I got Unhandled Exception:

FATAL_ERROR System.UnexpectedException: null

VF:

<apex:remoteObjectModel name="TopicAssignment" fields="Id" 
    update="{!$RemoteAction.ROT.updateNew}"/>
...
var topicIds = ['0FT7A0000008WlWWAU', '0FT7A0000008WlWWKl'];
new SObjectModel.TopicAssignment().update([<Array of IDS>],
    {mode:'saveTAssignment',plus:topicIds},callback);

Apex:

@RemoteAction
public static Map<String, Object> updateNew
    (String type, List<String> recordIds, Map<String, Object> fieldMap) { 
        String mode = (String)fieldMap.get('mode');//works fine
        fieldMap.get('plus')//throws exception
        JSON.deserialize(fieldMap.get('plus'),List<Id>.class)//throws exception

How to get the list or map or any JS object/literal passed in the remote object override call?

2
  • The way I read your error message, it appears that you can either have a plus array, a minus array, or both. Since you had a minus array and got the null exception, were you trying to remove that ID? You say you get the error only when passing an array. Please show us how you pass a single record sucessfully.
    – crmprogdev
    Commented Apr 26, 2016 at 15:42
  • @crmprogdev Think about a best case scenario where I am always passing all the values and still showing Error. Updated question how the ids are getting passed
    – Joy Biswas
    Commented Apr 26, 2016 at 19:24

1 Answer 1

2

Remote Method Overrides in Remote Objects example uses Map<String, Object> as third parameter in RemoteAction. That's useful, because we could send any primitive or any collection of primitives as an Object and deserialize it properly in Apex.

In your JS you're sending list<id> as an Object, apex could not deserialize collection automatically, that's why you have exception. You should serialize it with JSON.stringify or send plain string:

var ids = ['0FT3600000005cW', '0FT3600000005cZ'];
var plusIds = JSON.stringify(['0FT3600000005cY', '0FT3600000005cQ']); 
var jsonObj = '{"employees":[{"firstName":"John"},{"firstName":"Peter"}],
    "comments":[{"note":"stringify"}]}';
var ctDetails = {mode:'saveTAssignment',plus:plusIds,minus:jsonObj}  
var topicassignment = new SObjectModel.TopicAssignment().update(ids, ctDetails);

Deserialize in Apex:

list<String> ids =(List<String>)System.JSON.deserialize((string)fieldMap.get('plus'),List<String>.class);
Map<String,Object> objectMap = (Map<String,Object>)System.JSON.deserializeUntyped((String)fieldMap.get('minus'));
system.debug(objectMap.get('employees'));
system.debug(ids);

Debug:

DEBUG|(0FT3600000005cY, 0FT3600000005cQ)
DEBUG|({firstName=John}, {firstName=Peter})
6
  • I was using object coz the Map not always has List of strings sometimes it has List<object> sometimes sting etc, based on what APEX Object was build for to support datatype "ANY" internally. So I guess I have to use Object. The object can be cast to a list or string or Integer, isn't it the case
    – Joy Biswas
    Commented Apr 26, 2016 at 20:42
  • you can pass any json object within pair (string,object): plus:'John', plus:'{FirstName:"John", LastName: "Doe"}' plus:'{"employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"} ]}' and parse them accordlingly in apex Commented Apr 26, 2016 at 21:36
  • Please go through the question I am able to pass the multiple objects and the Map is displaying fine in System debug and in user debug when I want to print and .get() any key it gives me a System Exception(only if it is a JS object/Array, else it is fine for all primitive type). If I do .containsKey() it returns true
    – Joy Biswas
    Commented Apr 26, 2016 at 21:40
  • you should deserialize your string to collection of objects in class. I've added list<id> in answer Commented Apr 26, 2016 at 22:13
  • or try this Commented Apr 26, 2016 at 22:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .