Postman integration for posting a comment in a single ticket

dipanjansaha47
Tera Contributor

Hello,

 

In Field Service Management application, we have 1 work Order and under that Multiple work order tasks.

I am trying to create a Scripted Rest API to POST a comment from POSTMAN to a single work order task in ServiceNow.

JSON of the structure will be 

 

{
   "data":{
      "work_order":{
         "sysId":"b3a718a0eb8c1a109ee8f99bcad0cd7d",
         "tasks":[
            {
               "sys_id":"8cb710e0eb8c1a109ee8f99bcad0cd57",
               "comments":"Updated comments for the specific task."
            }
         ]
      }
   }
}

 

 

Also I am writing the POST code for the Scripted rest API as below

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    try {
        // Parse the JSON body of the request
        var data = request.body.data;

        // Validate the payload
        // if (!data || !data.task || !data.task.sysId || !data.task.comments) {
        //     throw new Error('Invalid request payload: Missing required fields');
        // }

        var taskSysId = data.task.sysId;
        var newComments = data.task.comments;

        // Log received data
        gs.info('Received sysId: ' + taskSysId);
        gs.info('Received comments: ' + newComments);

        // Create a GlideRecord object for the task table
        var grTask = new GlideRecord('wm_task');
        grTask.addQuery('sys_id', taskSysId);
        grTask.query();

        if (grTask.next()) {
            // Update the comments field
            grTask.comments = newComments;
            grTask.update();

            var responseBody = {
                result: 'Task updated successfully',
                taskSysId: taskSysId
            };

            response.setStatus(200);
            response.setBody(responseBody);
        } else {
            // Handle case where the task was not found
            gs.error('Task with sys_id ' + taskSysId + ' not found.');
            response.setStatus(404);
            response.setBody({
                error: 'Task not found'
            });
        }
    } catch (ex) {
        gs.error('Error processing request: ' + ex.message);
        response.setStatus(400);
        response.setBody({
            error: ex.message
        });
    }
})(request, response);

 

I am receiving the below error

 

{
  "result": {
    "error": "Invalid request payload: Missing required fields"
  }
}

 


Kindly help me to fix the script and the issue


Thanks in advance

Regards,

Dipanjan Saha

0 REPLIES 0