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 of the record . Can someone provide similar references to anything that they know? I guess I just need to put a small code

EKTA2
Tera Expert

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?

1 ACCEPTED SOLUTION

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.

find_real_file.png

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.

find_real_file.png

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

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

11 REPLIES 11

Hey Ekta,

Have you by mistake marked the incorrect response as correct? Please review, I have given the correct response with exact code which you need to use here in this scenario.

Regards,

Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

My bad! Just updated the correct response. Thank you, so much for helping Shloke.