0

The code below is running, i had to pass on the login credentials to Cloudingo because it is/was a stateless REST API call from SFDC to CLoudingo. Have to hardcode the key/password into the SFDC Code.

Thanks for everyone for their pointers. And i will still update the JSON code to create one JSON object to process. Separate i will add SFDC JSONPArser class code to process the response back into a SFDC Case object.

Below is the updated code i updated: Now i works!!!

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.cloudingo.com/rest/CheckRecordForDuplicates');                    
request.setMethod('POST');
//Set the header with the key and password to get access to cloudingo
request.setHeader('CloudingoIntegrationKey','xxxxxxxxxxxxxxx');
request.setHeader('CloudingoIntegrationPassword','yyyyyyyyyyyyyyyyy');
request.setHeader('Content-Type','application/json;charset=UTF-8');
//Set the body as a JSON object
request.setBody('{ \n' +  
'    "Fields" : [{  \n'+
'            "Field" : "Name",\n' +
'            "Data" : "Palo Alto Networks"\n'+             
'        }\n'+ 
'    ]  \n'+
'                }   \n'
);
//Parse the entire JSON response
HttpResponse response = http.send(request);
if(response.getStatusCode() != 201) {
    System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
    System.debug('Response ' + response.getBody());
} else {
    System.debug(response.getBody());
}
  • Please format the code in your post and explain why you believe this is a Salesforce issue. Doing both of these things will improve your chances of getting a helpful answer. – identigral Aug 11 '19 at 0:29
  • Is there a requirement that the JSON in your request needs to be formatted? It should be fine to use a compressed string and it would save yourself the trouble of trying to format the JSON with whitespace and newline characters. Once you've got it working you can leverage the JSONGenerator class to build these JSON strings dynamically. – nbrown Aug 11 '19 at 2:21
  • thanks, yes once it's working i will use the JSONgenerator class. This is currently only for small Proof of Concept (POC). Hence i just need the JSON to work, afterwards i can clean it up and use the JSONGenerator class. – Bart Sporken Aug 11 '19 at 2:47
  • Looking at your current code, you're missing commas in your JSON. Try replacing your setHeader argument with this: '[{"key":"CloudingoIntegrationKey","value":"f4a56572-030a-4d8c-871f-b1164aaed63f","type":"text"},{"key":"xxxxxxxxxxxxxxxxxxxxxxxx","value":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","type":"text"},{"key":"Content-Type","name":"Content-Type","value":"application/json","type":"text"}]' – nbrown Aug 11 '19 at 2:52
0

A string in Apex code must have a start apostrophe and an end apostrophe:

'                    [ { \n'+

Note that you also have problems with your JSON (missing commas), and the setHeader method requires two parameters, the key and value. You would not ordinarily put JSON inside an HTTP Header, so I suspect that you're doing something wrong.

Even more to the point, you should not be writing JSON this way. It is prone to errors (as I've already pointed out in one example). Instead, use JSON.serialize to create a properly crafted JSON object:

String jsonBody = JSON.serialize(
  new Object[] {
    new Map<String, Object> {
      'key' => 'CloudingoIntegrationKey',
      'value' => 'abcd',
      'type' => 'text'
    },
    new Map<String, Object> {
      'key' => 'CloudingoIntegrationPassword',
      'value' => 'abcd',
      'type' => 'text'
    }
  }
);

Please check the documentation and make sure it is formatted correctly.

  • Thanks you, will process this comment. Initially it was only a small JSON text pass on (call out) but it grew, hence now the JSON object and serialize make sense. FYI, it 's my first coding effort after a long hiatus in coding. – Bart Sporken Aug 11 '19 at 2:19

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.