1

Please help me to get this JSON string deserialize as map of list of string.

public static void postPermissionSetAutoDelegation(Map<string,Set<String>> delegationMap,String OrgName)
{
    String jsonstr = (String)JSON.serialize(delegationMap);

    //The JSonStr ->   {"ITL8090":["Administrator:TRUE","Bank_Operations_Reviewer:TRUE"]} 
    //Code skipped Retriving accessToken and instance URL

    String uri = ResponseMap.get('instance_url') + '/services/apexrest/PermSetDelegation/';
    System.debug(uri);
    //Post the data         
    HttpResponse res = send(uri,'HttpPost',jsonstr,ResponseMap.get('access_token')); //End point should be rest service URL
    System.debug('***'+res);
}

My Post method in another org

@HttpPost  
global static  Map<String, List<String>> processPermSetDelegation()
{
    RestRequest req = RestContext.request;
    Map<String, List<String>> delegationMap= (Map<String, List<String>>) JSON.deserializeUntyped(String.valueOf(req));  //Error         
    system.debug(req);
    return delegationMap;
    //Parse and send the data to handler class
} 

Error Message: RESPONSE_BODY[{"errorCode":"APEX_ERROR","message":"System.JSONException: Unexpected character ('R' (code 82)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]\n\nClass.System.JSON.deserializeUntyped: line 11, column 1\nClass.Method processPermSetDelegation: line 9, column 1"}]

  • Can you put in an example of your delegation map? – Caspar Harmer May 25 '17 at 6:43
  • {"ITL8090":["Administrator:TRUE","Bank_Operations_Reviewer:TRUE"]} – bharath May 25 '17 at 6:48
  • Pretty sure you need to get the body before trying to deserialize it - ie String reqBody = req.getBody(); then JSON.deserializeUntyped() it. I think the R` that you are getting is the first letter of the text "Request" – Caspar Harmer May 25 '17 at 6:54
  • Added as an answer. I'm fairly sure this will help you. – Caspar Harmer May 25 '17 at 6:58
1

You are currently trying to deserialize the RestRequst SObject to a Map. Instead, you want to deserialize the RestRequest's body, which contains the POSTed data. Here is the API page for the RestRequest class:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_restrequest.htm#apex_methods_system_restrequest.

Using the request body, your code would then look something like this:

@HttpPost  
global static  Map<String, List<String>> processPermSetDelegation()
{
    RestRequest req = RestContext.request;
    String requestBody = req.requestBody.toString();
    System.debug(requestBody);
    Map<String, List<String>> delegationMap = (Map<String, List<String>>) JSON.deserializeUntyped(requestBody);        
    System.debug(delegationMap);
    return delegationMap;
    //Parse and send the data to handler class
}

However, I advise posting params and then retrieving them via the RestRequest.params property.

1

Pretty sure you need to get the body before trying to deserialize it.

Try this:

@HttpPost  
global static  Map<String, List<String>> processPermSetDelegation(){
RestRequest req = RestContext.request;
Map<String, List<String>> delegationMap= 
    (Map<String, List<String>>) JSON.deserializeUntyped(String.valueOf(req.getBody()));    
....    

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.