- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2022 04:26 AM
I have a already created and working PUT method in Scripted REST API. I need to edit this API to get one additional field value. Can someone provide similar references to anything that they know? I guess I just need to put a small code in the already existing code : Gliderecord the table, pass sys id of the ticket and extract the field value?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2022 08:11 AM
Hi,
You need to update your code in two places:
1) You need to add below Line as shown after the line highlighted in Red below:
var automationResponse = requestString.AttributeName.toString(); // Replace "AttributeName" with the name of the Attribute 3rd party is sending in the payload.
Now you need to Update the field which you have created as Success or Error in Task table as shown below:
gr.FIELD_NAME_TO_Update = automationResponse; // Replace "FIELD_NAME_TO_Update" with the field name which you want to Update.
Also I have shared the complete code as well, where you just need to replace the highlighted areas as mentioned above:
var FulfilmentAutomationUpdateInterface = Class.create();
var util = new FulfilmentAutomationUtil();
FulfilmentAutomationUpdateInterface.prototype = {
initialize: function(response, sid, type, header) {
var config = util.coreConfig(sid, type, header); // check for core configurations
this.configFields = config;
if (JSUtil.doesNotHave(config.error)) {
if (type == "update") {
if (config.u_update) {
this.updateTicket = true;
} else {
return util.processError(response, 400, "Service account '" + gs.getUserDisplayName() + "' is not enabled for update operation.", "Update :" + config.u_update);
}
}
} else if (JSUtil.has(config.error)) {
return util.processError(response, 400, config.error, config.auth);
}
},
getResponse: function(request, response) {
if (this.updateTicket == true) {
var coreConfig = this.configFields;
var resDetails = {};
var integration_user_sys_id = gs.getUserID();
// Get the sys id of supplier of the integration user
var faObj = new FulfilmentAutomationUtil();
var integration_user_supplier = faObj.getIntegrationUserSupplier(integration_user_sys_id);
//GET ALL THE REQUEST PARAMETERS
var queryParameters = request.queryParams;
var taskID = request.pathParams.taskNumber;
var requestBody = request.body;
var requestString = requestBody.nextEntry();
var vState = faObj.getStates(requestString.state.toString());
var vSubState = faObj.getSubStates(requestString.substate.toString());
var vWorknotes = requestString.work_notes.toString();
var vClosure_notes = requestString.closure_notes.toString();
var vComments = requestString.additional_comments.toString();
var correlationID = requestString.correlation_id.toString();
var vAssigned_to = requestString.assigned_to.toString();
//var vAssignment_group = requestString.assignment_group.toString();
var attach_file_name = requestString.attach_file_name.toString();
var attach_file_type = requestString.attach_file_type.toString();
var attachment_base64 = requestString.attachment_base64.toString();
var automationResponse = requestString.AttributeName.toString(); // Replace "AttributeName" with the name of the Attribute 3rd party is sending in the payload.
if ((!taskID)) {
//return new sn_ws_err.BadRequestError('Please provide a Task Number.');
util.processError(response, 400, "Please provide a Task Number.", "Please provide a Task Number.");
return;
}
if (!vState && !vSubState && !vWorknotes && !vClosure_notes && !vComments && !correlationID && !vAssigned_to && !attach_file_name && !attach_file_type && !attachment_base64) {
//return new sn_ws_err.BadRequestError('The requested field cant be updated');
util.processError(response, 400, "The requested field can't be updated", "The requested field can't be updated");
return;
}
//1 = OPEN, -6 = QUEUED
if (vState == 1 || vState == -6) {
//return new sn_ws_err.BadRequestError('Update not allowed for state value (' + vState + ') supplied ' + requestString.state.toString());
util.processError(response, 400, "Update not allowed.", "Update not allowed for state value '" + vState + "' supplied " + requestString.state.toString());
return;
}
/*
if((!vState) ){
return new sn_ws_err.BadRequestError('Please provide a state value.');
}
*/
/*
//2 = WORK IN PROGRESS
if((!vSubState && vState != 2) ){
return new sn_ws_err.BadRequestError('Please provide a sub-state value.');
}
*/
if (vState.toString() == 'error') {
util.processError(response, 400, "Invalid state.", "Requested state in not valid '" + requestString.state.toString() + "'");
return;
}
if (vSubState.toString() == 'error') {
util.processError(response, 400, "Invalid substate.", "Requested substate in not valid '" + requestString.substate.toString() + "'");
return;
}
if (!vSubState && (vState == -5 || vState == 11)) {
//return new sn_ws_err.BadRequestError('Please provide a sub-state value.');
util.processError(response, 400, "Please provide a sub-state value.", "Please provide a sub-state value.");
return;
}
//3 = CLOSED COMPLETE
//if ((!vClosure_notes && vState == 3)) {
if (JSUtil.nil(vClosure_notes.trim()) && vState == 3) {
//return new sn_ws_err.BadRequestError('Please provide a closure notes if state is closed_complete.');
util.processError(response, 400, "Please provide a closure notes if state is closed_complete.", "Please provide a closure notes if state is closed_complete.");
return;
}
//Glide the table on which you want to post the record.
var gr = new GlideRecord('sc_task');
gr.addQuery('number', taskID);
//gr.addQuery('assignment_group.u_supplier',integration_user_supplier);
gr.addQuery('request_item.cat_item', 'IN', coreConfig.u_catalog_item);
gr.addQuery('assignment_group', 'IN', coreConfig.u_support_group);
//gr.addActiveQuery();
gr.setLimit(1);
gr.query();
if (gr.next()) {
if (vState && vSubState) {
if ((vState == 11) && (vSubState != 11 && vSubState != 5 && vSubState != 6 && vSubState != 20 && vSubState != 21 && vSubState != 22 && vSubState != 23 && vSubState != 24 && vSubState != 25 && vSubState != 26 && vSubState != 27 && vSubState != 28)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((vState == -5) && (vSubState != 16 && vSubState != 14 && vSubState != 8 && vSubState != 12 && vSubState != 13 && vSubState != 9 && vSubState != 10 && vSubState != 15)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((vState == 3) && (vSubState != 29)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((vState == 7) && (vSubState)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((vState == 2) && (vSubState)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((vState == 4) && (vSubState)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((vState == 1) && (vSubState)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((vState == -6) && (vSubState)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
}
if (!vState && vSubState) {
if ((gr.state == 11) && (vSubState != 11 && vSubState != 5 && vSubState != 6 && vSubState != 20 && vSubState != 21 && vSubState != 22 && vSubState != 23 && vSubState != 24 && vSubState != 25 && vSubState != 26 && vSubState != 27 && vSubState != 28)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((gr.state == -5) && (vSubState != 16 && vSubState != 14 && vSubState != 8 && vSubState != 12 && vSubState != 13 && vSubState != 9 && vSubState != 10 && vSubState != 15)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((gr.state == 3) && (vSubState != 29)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((gr.state == 7) && (vSubState)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((gr.state == 2) && (vSubState)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((gr.state == 4) && (vSubState)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((gr.state == 1) && (vSubState)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
if ((gr.state == -6) && (vSubState)) {
util.processError(response, 400, "Invalid state and substate combination", "Invalid state and substate combination");
return;
}
}
if (vState != -6) {
//2 = WORK IN PROGRESS, 1 = OPEN, -5 = PENDING, 11 = AWAITING INFO
if (vState == 2 && (gr.state != 1 && gr.state != 2 && gr.state != -5 && gr.state != 11)) {
//return new sn_ws_err.BadRequestError('Update not allowed for state value (' + vState + ') supplied ' + requestString.state.toString());
util.processError(response, 400, "Update not allowed.", "Update not allowed for state value '" + vState + "' supplied " + requestString.state.toString());
return;
}
//3 = CLOSED COMPLETE, 4 = CLOSED IN COMPLETE, 7 = CLOSED CANCELLED, 2 = WORK IN PROGRESS
if ((vState == 3 || vState == 4 || vState == 7) && (gr.state != 2)) {
//return new sn_ws_err.BadRequestError('Update not allowed for state value (' + vState + ') supplied ' + requestString.state.toString());
util.processError(response, 400, "Update not allowed.", "Update not allowed for state value '" + vState + "' supplied " + requestString.state.toString());
return;
}
//-5 = PENDING, 1 = OPEN, 2 = WORK IN PROGRESS, 3 = CLOSED COMPLETE
if (vState == -5 && (gr.state != 1 && gr.state != 2 && gr.state != 3)) {
//return new sn_ws_err.BadRequestError('Update not allowed for state value (' + vState + ') supplied ' + requestString.state.toString());
util.processError(response, 400, "Update not allowed.", "Update not allowed for state value '" + vState + "' supplied " + requestString.state.toString());
return;
}
//11 = AWAITING INFO, 2 = WORK IN PROGRESS, 1 = OPEN
if (vState == 11 && (gr.state != 2 && gr.state != 1)) {
//return new sn_ws_err.BadRequestError('Update not allowed for state value (' + vState + ') supplied ' + requestString.state.toString());
util.processError(response, 400, "Update not allowed.", "Update not allowed for state value '" + vState + "' supplied " + requestString.state.toString());
return;
}
/*
//2 = WORK IN PROGRESS
if((gr.state != 2) ){
return new sn_ws_err.BadRequestError('Please provide a sub-state value.');
}
*/
if (vSubState) { //Only update if sub state is provided
gr.u_sub_state = vSubState;
}
if (vState == 2) { //If state is WORK_IN_PROGRESS, update substate to empty
gr.u_sub_state = '';
}
gr.work_notes = vWorknotes;
if ((vState)) { //Only update if state is provided
gr.state = vState;
}
// adding assignment group and assigned to
if (vAssigned_to) {
gr.assigned_to = this.getAssigned_to(vAssigned_to);
}
// if (vAssignment_group) {
// gr.assignment_group = this.getAssignment_group(vAssignment_group);
// }
gr.u_closure_notes = vClosure_notes;
gr.correlation_id = correlationID;
gr.FIELD_NAME_TO_Update = automationResponse; // Replace "FIELD_NAME_TO_Update" with the field name which you want to Update.
gr.comments = vComments;
gr.update();
}
if (vState == -6) {
gr.work_notes = vWorknotes;
gr.update();
}
var fulfilmentAttach = new FulfilmentAutomationAttachUtil();
//This change is because, this particular line is calling the 'addAttachment' function in the 'FulfilmentAutomationAttachUtil' script include.
//As per the commented code , parameters such as 'tableName' and 'response' were missed, hence the attachments were not created. Hence the 'tableName' parameter is passed as null('') because the tableName will be set to sc_task when it is passed as empty.
//Refer 'addAttachment' function in the 'FulfilmentAutomationAttachUtil' script include.
//var result = fulfilmentAttach.addAttachment(taskID, attach_file_name, attach_file_type, attachment_base64);
var result = fulfilmentAttach.addAttachment(taskID, '', attach_file_name, attach_file_type, attachment_base64, response);
resDetails = {
"number": gr.number.getDisplayValue(),
"state": gr.state.getDisplayValue(),
"sys_updated_by": gr.sys_updated_by.getDisplayValue(),
"priority": gr.priority.getDisplayValue()
//"Message": taskID + " is updated successfully."
};
} else {
// resDetails = {
// "Message": taskID + " is not found or not active task.",
// };
util.processError(response, 404, "Task number provided in request is not found or not active task.", taskID);
return;
}
response.setContentType('application/json');
response.setStatus(200);
response.setBody(resDetails);
var writer = response.getStreamWriter();
return writer.writeString(JSON.stringify(resDetails));
}
},
getAssigned_to: function(assigned_to) {
var gr = new GlideRecord("sys_user");
gr.addQuery("user_name", assigned_to).addOrCondition("email", assigned_to);
gr.addActiveQuery(); // filter out inactive users
gr.setLimit(1);
gr.query();
if (gr.next()) {
//return gr.sys_id;
return gr.getValue('sys_id');
}
},
// getAssignment_group: function(assignment_group) {
// var gr = new GlideRecord("sys_user_group");
// gr.addQuery("name", assignment_group);
// gr.addActiveQuery(); // filter out inactive users
// gr.setLimit(1);
// gr.query();
// if (gr.next()) {
// //return gr.sys_id;
// return gr.getValue('sys_id');
// }
// },
type: 'FulfilmentAutomationUpdateInterface'
};
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2022 04:37 AM
Hi,
Can you share your current code what you have written?
If you are getting the object value in Scripted Rest API and based on which you need to fetch another value then you can do a Glide Record as shown below:
var gr = new GlideRecord('Table Name');
gr.addQuery('Field Name against which comparision need to be done','Value based on which you want to check');
gr.query();
if(gr.next()){
var neededValue = gr.FieldName; (which you want to retrieve)
}
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2022 04:57 AM
Hi, this is some portion of the code. They are getting some response as well as part of the attempt.
There is a function to get "Assigned to " value.
getAssigned_to: function(assigned_to) {
var gr = new GlideRecord("sys_user");
gr.addQuery("user_name", assigned_to).addOrCondition("email", assigned_to);
gr.addActiveQuery(); // filter out inactive users
gr.setLimit(1);
gr.query();
if (gr.next()) {
//return gr.sys_id;
return gr.getValue('sys_id');
I guess I can write same kind of function to get the field value I want. That means the Team which will be using this Scripted REST API will have the understanding that they need to use the function ( which I will create) to POST that value to servicenow?
Sorry new to Integration.... trying to jot down the pieces.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2022 05:19 AM
Nope function how you are handling it within ServiceNow is something a ServiceNow admin need to look at. 3rd party system should not be wondering how you want to handle it.
path it should be approached is format and type of data being sent to you so that you can handle it.
Like in the example for the function code you shared, let me take that example only and explain it to you:
getAssigned_to: function(assigned_to) {
var gr = new GlideRecord("sys_user");
gr.addQuery("user_name", assigned_to).addOrCondition("email", assigned_to);
gr.addActiveQuery(); // filter out inactive users
gr.setLimit(1);
gr.query();
if (gr.next()) {
//return gr.sys_id;
return gr.getValue('sys_id');
So if you see in first line that looks like a Function written in Script Include where the function is expecting a parameter value named "assigned_to" based on which it is doing a Glide Record on User Table and validating and returning the User record found.
Now it depend what is being passed into "assigned_to" is it a sys_id there or a User name being passed and based on that value confirmation you can do a GLide Record add Query.
I would ask you share more details if you are looking for code and can assist you.
Hope the above explanation makes sense.
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2022 05:42 AM
Thanks for explaining. So I have a Task Table and there is a field called "Automation response" in it. 3rd Party system will be passing in a value "Success" / "Error" if it Successfully completed the Task assigned or if it could not complete the task assigned to it.
I have been asked that I need to edit the already existing POST method in the Scripted REST API being used by 3rd party system so that they can pass value of automation response field i.e. "Success" or "Error" ( which is available on task table).
What I understood is I need to create an End Point for them ( not sure if I am right, probably end Point is already created as API exists).
Also, this Scripted Rest API can only be accessed via Service account. So I need to impersonate that service account to test wth rest API explorer?