I'm performing a REST API call in Salesforce from DialogFlow fulfilment but I just can not read the JSON response. I'd like to access some fields of the JSON response esp CaseNumber. The query can return 0 or several records.
Could someone help me just to parse the returned response? Thanks!
function ticket(agent) {
agent.add('Looking for ticket --> ' + agent.parameters.numero);
var rpTicket = require('request-promise-native');
var optionsTicket = {
method: 'GET',
uri: "https://eu26.salesforce.com/services/data/v44.0/query?q=select CaseNumber from Case where ContactID='XXXXXXX'+order+by+CreatedDate+desc",
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer XXXXXXXXXX'
},
json: true
};
return rpTicket( optionsTicket )
.then( body => {
console.log( 'Ok here is the JSON --> ' + util.inspect(body,false,null)); // I can read the JSON response in the Google Log console so I m fine
var myTickets = JSON.parse(util.inspect(body,false,null));
// or
//var myTickets = JSON.parse(body);
console.log( 'Ok here is the # of tickets --> ' + myTickets.count); // Throws an error
return Promise.resolve( true );
})
.catch( err => {
// You should also return a message here of some sort
console.log( 'Error ... --> ' + err);
return Promise.resolve( true );
});
}