cancel
Showing results for 
Search instead for 
Did you mean: 

xsjs 500 internal server error

0 Kudos

Hi

I'm trying to create my first XSJS file and when trying to run it I am getting "500 Internal server error"

500.png

Here is the code that I am trying to execute:

itsm.png

var destination_package = "itsm.services";
var destination_name = "Destination";
var custnum = $.request.body.asString();
var essai = custnum.toString().replace("\r", "").replace("\n", "").replace("\t", "");
essai = JSON.parse(essai);
var Prioritytickt = essai.Prioritytickt;

try {
	var dest = $.net.http.readDestination(destination_package, destination_name);
	var client = new $.net.http.Client();
	var req = new $.web.WebRequest($.net.http.GET, "MessageResultSet?$format=json"); //Entity Set 
	client.request(req, dest);
	var response = client.getResponse();

	$.response.contentType = "application/json";
	var result = response.body.asString();
	var jsonResult = JSON.parse(result);
	var chatbotResponse;

	if (jsonResult.d.results.length > 0) {

		if (Prioritytickt === "2") {
			chatbotResponse = "Here are the high priority tickets ";
		}
		if (Prioritytickt === "3") {
			chatbotResponse = "Here are the medium priority tickets ";
		}
		if (Prioritytickt === "4") {
			chatbotResponse = "Here are the low priority tickets ";
		}
		if (Prioritytickt === "All") {
			chatbotResponse = "List of all the tickets ";
		}

		if (Prioritytickt !== "All") {
			var ticketdetails = jsonResult.d.results.filter(function(hero) {
				return hero.Priority === Prioritytickt;
			});
			for (var i = 0; i < ticketdetails.length; i++) {

				chatbotResponse += ticketdetails[i].ObjectId;
				if (i < ticketdetails.length - 1) {
					chatbotResponse += ", ";
				}

			}
		} else {
			for (var i = 0; i < jsonResult.d.results.length; i++) {

				chatbotResponse += jsonResult.d.results[i].ObjectId;
				if (i < jsonResult.d.results.length - 1) {
					chatbotResponse += ", ";
				}

			}
		}

		var output = {
			replies: [{
				type: 'text',
				content: chatbotResponse
   					}],

			conversation: {
				memory: chatbotResponse
			}
		};
	} 
	var body = JSON.stringify(output);
	$.response.contentType = 'application/json';
	$.response.setBody(body);
	$.response.status = $.net.http.OK;

} catch (e) {
	$.response.contentType = "text/plain";
	$.response.setBody(e.message);
}

Please advice.

Regards,

Florence

Accepted Solutions (0)

Answers (1)

Answers (1)

Ivan-Mirisola
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi ,

You are probably not sending anything to $.request.body.

Since this is not being caught by your try...catch block, your server side script is failing with an internal error.

Try sending something in the request's body to see if it shows up.

Otherwise, I'd recommend enabling XSJS debugging so you can set breakpoints on your code to inspect them before the error has been risen.

Best regards,
Ivan