Returning empty response object

Eash1
Tera Expert

Hello,

I am using scripted rest API to do some close operation. I am trying to generate response object from a different function and return it back but it does return only empty "". When i am trying to print it does work.

 

Code - 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var LOG_SOURCE = 'REQUEST API:POST:CLOSE CTASK';
var reqBody = request.body.data;
var taskNumber = request.body.data.task_number;
var workNotes = request.body.data.work_notes;
var state = request.body.data.state;
var successMessage = "Task update successful";
var failureMessage = "Task update failure. Please raise an incident to ESM-Frontline and provide message body and response.";
var inValidMessage = "Not a valid Catalog Task Number. Please provide a valid catalog task number associated to RITM";
var responseObj = {};

if(!reqBody.hasOwnProperty('task_number') || !reqBody.hasOwnProperty('state') ){
responseObj= generateResponse(2,taskNumber, "Please provide a valid Task# and State value");
return responseObj;
}

// Get the target change number from the request body.
var accessKey = request.body.data.access_key;

// Verify API Customer
var apiAuthorization = new APIAuthorization(request, accessKey, null);
if (!apiAuthorization.isAuthorized()) {
gs.log('Error: ' + accessKey + ' not authorized', LOG_SOURCE);
return {
status: 2,
responseText: apiAuthorization.message()
};
}

//Updat Catalog Task
updateTask(taskNumber,workNotes,state);

function updateTask(taskNumber,workNotes,state){
try{
var cTaask = new GlideRecord("sc_task");
cTaask.addEncodedQuery('active=true^number='+taskNumber);
cTaask.query();

if(cTaask.next()){
cTaask.work_notes = workNotes;
cTaask.state = state;
cTaask.update();

responseObj = generateResponse(1,taskNumber,successMessage);

}else{
responseObj = generateResponse(2,taskNumber,inValidMessage);
}
gs.info("responseObj : " + responseObj); //DOES PRINT CORRECT
return responseObj;  //DOES NOT RETURN
}
catch(e){
gs.error("Task update failure for " + taskNumber, LOG_SOURCE);
responseObj= generateResponse(2,taskNumber,failureMessage);
return responseObj;
}

}

//Generate Catalog Item Response
function generateResponse(statusCode,number,message){
gs.info("inside response body method : " + message);
return JSON.stringify({ 'status': statusCode.toString(), 'Task#': number.toString(), 'responseText': message.toString()});
}



})(request, response);

 

Thank you,

Eash

1 ACCEPTED SOLUTION

Ashutosh Munot1
Kilo Patron
Kilo Patron

 

Check this: https://community.servicenow.com/community?id=community_question&sys_id=626183f4dbd8c414f7fca851ca9619f7

 

Thanks,
Ashutosh

View solution in original post

1 REPLY 1

Ashutosh Munot1
Kilo Patron
Kilo Patron

 

Check this: https://community.servicenow.com/community?id=community_question&sys_id=626183f4dbd8c414f7fca851ca9619f7

 

Thanks,
Ashutosh