$request method crashing

Hello. I am trying to create an app that automatically updates the summary of a ticket when a private note is added in certain format.

I do not know why, but it is crashing.

Find the code below:

exports = {
 
  events: [
    { event: 'onConversationCreate', callback: 'onConversationCreate' }
  ],
 
  // args is a JSON block containing the payload information.
  // args['iparam'] will contain the installation parameter values.
 
 
  onConversationCreate: function(args) {
    //Only apply that for notes
    console.log("New conversation. Source: "+ args['data']['conversation']['source']);
    if (args['data']['conversation']['source']===2){
      var data = args['data']['conversation']['body_text'];
      if (data.includes("[Basic summary]:")){
        console.log("Includes [Basic summary]: tag");
        var TicketID = args['data']['conversation']['ticket_id'];
        var i = data.indexOf("[Basic summary]:");
        var j = data.indexOf("[Next Steps]:");
        var payload = data.substring(i,j);
        //var EncodedAuthentication= btoa(client.iparams.get(api_key)+":X");
        var EncodedAuthentication= "ThatIsTheKeyEncoded";
 
        //Get previous Summary
        var options = {
          'method': 'GET',
         'url': 'https://COMPANY.freshdesk.com/api/v2/tickets/' + TicketID + '/summary',
          'headers': {
            'Content-Type': 'application/json',
            'Authorization': 'Basic '+EncodedAuthentication
          }
        };
        console.log(options);
        console.log("PAPA1");
        $request(options, function (error, response) {
          console.log("PAPA2");
          if (error) throw new Error(error);
          console.log(response.body);
          var getReply = JSON.parse(response.body);
          console.log(getReply);
          var summary = getReply['body_text'];
          summary.concat("<br><br>");
          summary.concat(payload);
 

          //Update the Summary
          var options = {
            'method': 'PUT',
            'url': 'https://COMPANY.freshdesk.com/api/v2/tickets/'+ TicketID +'/summary',
            'headers': {
              'Content-Type': 'application/json',
              'Authorization': 'Basic '+EncodedAuthentication
            },
            body: JSON.stringify({
              "body": summary
            })
 
          };
          $request(options, function (error, response) {
            if (error) throw new Error(error);
            console.log(response.body);
          });
 
        });
 
      }
     }
  }
  };

It never reaches the PAPA2 log, crashing before:

I have the same kind of GET function on another app and it is working.
I have tested to twist a little bit the request and removing the METHOD on the options, but switching the $request to: $request.get(…) No crash is noticed then but still not processing the next console.log, it simplies does not goes on on the script.

Does anybody knows what might be going on?

Thanks in advance.

Hi @Chavas

You will have to specify the right function for the $request object similar to $request.get and/or $request.post etc. to make API calls using the request method.

Please try this and let me know if you still face this issue.

1 Like

Hi. I tried to do so, but the problem than adding that $request.get does not provide any kind of output and neither goes on the console.log. It looks like it gets stucked there.

Can you try using this @Chavas.

var EncodedAuthentication= btoa(client.iparams.get(api_key)+":X");
var TicketID = args['data']['conversation']['ticket_id'];
var url = 'https://COMPANY.freshdesk.com/api/v2/tickets/' + TicketID + '/summary',

var options = {
    'headers': {
        'Content-Type': 'application/json',
        'Authorization': 'Basic ' + EncodedAuthentication
    }
};

$request.get(url, options).then(function (data) {
    console.log(data);
  }, function (error) {
    console.log(error);
  });

I just tried this on a test app and it worked properly as expected with the response from the ticket being logged correctly.

3 Likes

Lovely. That solved it.

Many thanks Arjun!

1 Like