3

I can't work out why I'm getting this error. Ultimately, I'm just trying to send a list of SingleEmailMessage via callout, but that's not a valid type for the post method nor can it be directly serialized/deserialized.

RestResource method:

@HttpPost
global static void sendEmails(List<String> emailJSONList) {
    //all code is commented out, error still occurs
}

Callout method:

@Future(Callout=true)
public static void sendExceptionEmails(List<String> emailJSONList) {
    HttpRequest req = new HttpRequest();
    req.setMethod('POST');
    req.setEndpoint('callout:**NamedCredential**/services/apexrest/TestingSomething');
    req.setHeader('Content-Type','application/json;charset=UTF-8');
    req.setBody('{"emailJSONList":'+String.join(emailJSONList,',')+'}');

   System.debug('{"emailJSONList":'+String.join(emailJSONList,',')+'}');

    Http http = new Http();
    HttpResponse res = http.send(req);
    System.debug(res.getBody());
}

Debug (I've replaced the < and > with ^ since it was screwing up the formatting):

10:31:33.9 (11162732)|USER_DEBUG|[38]|DEBUG|{"emailJSONList":[{"bccAddresses":null,"bccSender":false,"ccAddresses":null,"charset":null,"debug":"subject: testsubject0, bccSender: false, saveAsActivity: true, useSignature: true, toAddresses: [test@example.com], htmlBody: ^p^test Body^/p^,", "emailPriority":"Normal","entityAttachments":null,"fileAttachments":null,"htmlBody":"^p^test Body^/p^","inReplyTo":null,"numRecipients":1,"optOutPolicy":"SEND","orgWideEmailAddressId":null,"plainTextBody":null,"references":null,"replyTo":null,"saveAsActivity":true,"senderDisplayName":null,"sentDate":null,"subject":"testsubject0","targetObjectId":null,"templateId":null,"templateName":null,"toAddresses":["test@example.com.com"],"treatBodiesAsTemplate":false,"treatTargetObjectAsRecipient":true,"useSignature":true,"userMail":false,"whatId":null}]}

10:31:33.9 (983471670)|USER_DEBUG|[41]|DEBUG|[{"message":"Illegal value for primitive at [line:1, column:20]","errorCode":"JSON_PARSER_ERROR"}]

There are no parsing issues when I paste it into https://codebeautify.org/jsonviewer.

  • 5
    The problem, I think is in the parameter you pass in. The Apex function sendEmails expects this to be a list, but in the call you pass in a String (because you use String.join(emailJSONList, ','). Maybe you should try using a normal Map in the calling function and serialize that into JSON input to the function. – rael_kid Jun 17 '18 at 18:26
  • 1
    @rael_kid Thanks! That did it :) Do you mind posting that as an answer so I can mark as best? – Mike Jun 17 '18 at 18:54
2

The problem, I think is in the parameter you pass in. The Apex function sendEmails() expects this to be a list, but in the call you pass in a String (because you use String.join(emailJSONList, ','). Maybe you should try using a normal Map in the calling function and serialize that into JSON input to the function.

So you could use something like this in your calling class:

Map<String, String> params = new Map<String, String>();
params.put('emailJSONList', JSON.serialize(emailJSONList));
req.setBody(JSON.serialize(params));
  • Thanks! I ended up creating a Map<String,List<String>> variable, putting into it the string "emailJSONList" and param emailJSONList, then serializing it as the body. Worked like a charm. – Mike Jun 18 '18 at 14:30

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.