1

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 );
    });     
  }
Michael Nelles
  • 5,426
  • 8
  • 41
  • 57

1 Answers1

0

Since you have json: true in your request-promise configuration, the body that is returned is already a JavaScript object. So you do not need to call JSON.parse(body).

You should just be able to read the value you want with body.count (assuming count is a field that is returned).

Prisoner
  • 49,922
  • 7
  • 53
  • 105