- 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 06:06 AM
Nope I don't think you need to create an end point. End point might be there already, so you need to figure out in your instance which is the scripted Rest API which is curretnly being used to create Record in Task Table.
So within the same scripted Rest API method under Resources tab you need to update this field as well as Success or any other value.
To help you in right direction I will need two things here:
1) Screenshot of your Scripted Rest API where it contains the code where it is creating record.
2) Sample data structure which 3rd party is sending to you.
You can refer the below link to understand more on how to start with Integration and understand more.
https://community.servicenow.com/community?id=community_article&sys_id=30f8a1631b5c4910cd3b33bc1d4bcbbf
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 07:08 AM
This is the Method that I have been asked to Update .
Third Party just need to pass across the value of "Automation response" field. the value can either be "Success" or "Error".
What I think IS I just need to add one more function getautomatiionresponse in the end, just like assignment group is given? and they will be abel to use that function to send the value across?
And below is the entire code. I pasted all, so that you can grab whatever you need to understand the situation here.
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();
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.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'
};
- 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 08:22 AM
Thank you, Shloke. Now I have the full understanding.
When I am trying to test this via API explorer, it is giving me error that only service account has permission to so. Authentication is checked for this API and ACL is also given. When I check that ACL there is JUST basic case that external users cant access it, rest everyone can. I do not seem to find the Service account which has the permission for this account.
Can you please also let me know how TO test it using REST API Explorer? I assume I need to impersonate the service account which has permissions to test?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2022 09:10 AM
Hi,
This is happening because of your existing code written in the same script shared above:
In order to debug this further I would suggest to look at the first line of code i.e.:
var util = new FulfilmentAutomationUtil();
where "FulfilmentAutomationUtil" is another Script Include which you need to open and then within this Script Include check for the function name "coreConfig". So now within this function there should be a logic written for "u_update" and when you are trying to test if the value of Type is not the "Update" then it will throw the error you are getting defined in the same script as seen below:
So would suggest to check on above points.
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke