6

I have an instance where I am trying to pass a dynamic number of Page Parameters into an action method. It works... but it only will pass as a String. Is there a way to pass correclty as a Map? (The String version clearly looks like it was meant to be a Map but when passing as a Map I get an error saying that it was passing a String.)

An acceptable alternative would also be an easy way to convert Strings to Maps but I would prefer being able to directly pass a Map.

  • 1
    The documentation says "collections" are supported which I would interpret to include maps. But maybe the problem is the same one that afflicts @RestResource signatures where maps of objects are not supported. I guess passing the map as a JSON string is a work-around given that Apex has decent JSON support (and of course so does JavaScript). – Keith C Jun 8 '15 at 17:03
  • @KeithC Hmmmm I might look into doing the JSON version... although I'm already almost done writing the code that will parse a String into a Map. – dphil Jun 8 '15 at 17:06
  • 2
    A good thing about JSON is that escaping of embedded quotes or line feeds in strings is taken care of. – Keith C Jun 8 '15 at 17:13
  • 2
    JSON is absolutely better than rolling your own parser. – Adrian Larson Jun 8 '15 at 17:19
2

I ended up using JSON to parse it:

String jsonPageParams{get; private set;}

...

  jsonPageParams = JSON.serialize(currPageParams);

...

  Map<String, Object> parsedParams = (Map<String, Object>)JSON.deserializeUntyped(jsonPageParams);

I had to use a Map of String, Object for some reason even though it's technically a Map. I just have to cast the result to a String whenever I get the value.

| improve this answer | |
-1

I know this has been answered but I would like to comment anyway.

The way I handled it is by passing Keys and Values as string and recreating the Map in apex.

Here is an excerpt:

So if I were to pass a map through JS, I would send the keys and values as separate JS Array types. Now, note that remoting lets us pass the List(array) of sObject, so the values can be a List Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.jsRemotingControllerv1.createAccountsMap}', keys, accounts_values);

Over at the contoller side, I'd recreate the map like this:

Map<String,Account> accountMap = new Map<String,Account>();
for (Integer i = 0 ; i < keys.size(); i++) {
    accountMap.put(keys[i],a[i]);
}

This example may not be depicting a real lide scenario but it gives an alternative when we no choice but to pass a Map around.

For the complete code, you can see my post here https://salesforcemann.wordpress.com/2016/11/16/javascript-remoting-create-account-record-using-single-sobject-list-map/

| improve this answer | |
  • 1
    link only answers usually get deleted. The contents of your link need shown on your post. – dphil Nov 16 '16 at 12:56
  • Oops.. okay will update it soon... – human Nov 16 '16 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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