Is it possible to change the user that comments on a ticket when doing a GlideRecord update?

lee9
Kilo Contributor

When ServiceNow receives a REST message to update a comment on a ticket, it adds the comment as the "Guest" account. Instead, I would like to add the comment to the ticket as the user who sent the REST message to ServiceNow. Is there a way to tell GlideRecord to update as another user. For example, is there a way to update a record via script just like you can while you are impersonating another user? It would be nice to have the activity log on the ticket show the correct user that left the comment.

Thank you.

 

12 REPLIES 12

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Lee,

It is possible to specify the user to post incident work notes via REST API by creating a Scripted REST API.

A simple GET API to post notes by uri.

https://<servicenow instance name>.service-now.com/api/<API namespace>/postincidentworknote?incident=<incident number>&message=<message text>&user=<user id>

 

Script

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    try {
        var number = request.queryParams.incident.toString();
        var message = request.queryParams.message.toString();
        var userName = request.queryParams.user.toString();
        var grInc = new GlideRecord('incident');
        if (grInc.get('number', number)) {
            var now_GR = new GlideRecord("incident");
            now_GR.addQuery("number", number);
            now_GR.query();
            if (now_GR.next()) {
                now_GR.work_notes.setJournalEntry(message, userName);
                now_GR.update();
            }
        }
        return {
            "requestString": number
        };
    } catch (e) {
        return {
            "requestString": e.message
        };
    }
})(request, response);

Example:

https://<servicenow instance name>.service-now.com/api/<API namespace>/postincidentworknote?incident=INC0000002&message=scripted api test&user=abel.tuter

 find_real_file.png

Using POST. 'postincidentworknote' is the name of scripted rest api.

curl  --user '<username>':'<password>' -X POST -H "Content-Type: application/json" -d '{"number":"<incident number>", "message":"<message to post>", "username":"<user id of user posting>"}' https://<instance name>.service-now.com/api/<namespace>/postincidentworknote
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    try {
        var requestData = request.body.data;
        if (requestData instanceof Array) {
            for (var i = 0; i < requestData.length; i++) {
                var number = requestData[i].number;
                var message = requestData[i].message;
                var userName = requestData[i].username;
                addWorkNote(number, message, userName);
            }
        } else {
            var number2 = requestData.number;
            var message2 = requestData.message;
            var userName2 = requestData.username;
        }
        addWorkNote(number2, message2, userName2);
        return {
            "message": 'OK'
        };
    } catch (e) {
        return {
           "message": e.message
        };
    }

    function addWorkNote(number, message, username) {
        var grInc = new GlideRecord('incident');
        if (grInc.get('number', number)) {
            var now_GR = new GlideRecord("incident");
            now_GR.addQuery("number", number);
            now_GR.query();
            if (now_GR.next()) {
                now_GR.work_notes.setJournalEntry(message, username);
                now_GR.update();
            }
        }
    }
})(request, response);

Hi,

Thank you! it can work even if the user do not have servicenow user?