0

An external vendor is requiring to pass an array/list in order to send data from Salesforce Account to their system. They provided me with the code shown under request.setbody, but I am getting an error on 'fields'. Can anyone help?

Line: 6, Column: 1 Invalid identifier ' '. Apex identifiers must start with an ASCII letter (a-z or A-Z) followed by any number of ASCII letters (a-z or A-Z), digits (0 - 9), '$', '_'.

Http http = new Http();
HttpRequest request = new HttpRequest();

request.setEndpoint('https://xxxxxxx/login/sso/salesforce?env=xxxxxxxx&aftSubmitURL=rest/v1/witems.json?namespaceId=SALESFORCE&async=true&addRequiredForms=true');

request.setMethod('POST');

request.setBody('{
  "fields" : [ {
    "name" : "PARAM.REQUESTGROUP",
    "value" : [ "New Account" ]
  }, {
    "name" : "PARAM.PRODUCTTYPE",
    "value" : [ "Direct - Advisory" ]
  }, {
    "name" : "Account1.Id",
    "value" : [ "9999900001" ]
  }, {
    "name" : "Account1.FirstName",
    "value" : [ "John" ]
  }, {
    "name" : "Account1.LastName",
    "value" : [ "AFFOLTER1" ]
  }, {
    "name" : "Account1.SocSecNumber__c",
    "value" : [ "231689353" ]
  }, {
    "name" : "Account1.SocSecNumber__c",
    "value" : [ "231689353" ]
  }, {
    "name" : "Account1.Rep_Code__c",
    "value" : [ "11416001" ]
  }, {
    "name" : "PARAM.REGTYPE",
    "value" : [ "529 Plan" ]
  }, {
    "name" : "PARAM.VENDOR",
    "value" : [ "AMERICAN FUNDS" ]
  } ]
}');
2
  • I added a parentheses and a single quote to your setBody method which is needed when calling that method in Apex Commented Jan 14, 2021 at 14:07
  • Thanks for the quick help. I added your edit and I am now getting the following error:Line: 6, Column: 17 Illegal string literal: Line breaks are not allowed in string literals Commented Jan 14, 2021 at 14:16

1 Answer 1

1

Like the error you're getting says, you can't have a string literal (text enclosed inside of single quotes 'like this') span multiple lines.

Bad:

String myStr = 'Haikus are easy
but sometimes don\'t make much sense
refridgerator';

If you need to have a multiline string (if for nothing but keeping things organized and easy to read), then you need to have a separate string literal for each line and concatenate them

String myStr = 'Haikus are easy\n'
+ ' but sometimes don\'t make much sense\n'
+ ' refridgerator';

You do have to worry about adding newlines/line-breaks and leading/trailing spaces yourself though.

In your case, since you're trying to generate JSON for the request body, having a simple helper class and using JSON.serialize() is probably the easiest and most reliable way to go about this.

// This little helper class represents the structure of a single field;
//   "name" is a string, and "value" is a list in the JSON you're trying to generate
// The constructor helps make it easy to use without needing to use things like
//   new Map<String, Object>{} and new List<String>{} over and over
public class Field{
    public String name;
    public List<String> value;

    public Field(String inName, String inValue){
        name = inName;
        value = new List<String>{inValue};
    }
}

JSON.serialize(new Map<String, Object>{
    'fields' => new List<Field>{
        new Field('PARAM.REQUESTGROUP', 'New Account'),
        new Field('PARAM.PRODUCTTYPE', 'Direct - Advisory'),
        // and so on...
        new Field('PARAM.VENDOR', 'American Funds')
    }
});

You must log in to answer this question.

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