Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

closed incident showing users name instead of system servicenow

TarunChowdary
Tera Contributor

Hi Community,

 

I'm using a scheduled job to check the resolved incidents and close them after 5 weekdays with this code

 

updateRecords();
function updateRecords() {
    try {
        var inc = new GlideRecord('incident');
        inc.addQuery('state', 6); // State 6: Resolved
        inc.query();

        while (inc.next()) {
            var resolvedAt = new GlideDateTime(inc.resolved_at);
            var nowTime = new GlideDateTime();

            var days = getDateDiffExcWeekends(resolvedAt, nowTime);
           
            // If 5 business days have passed since resolution
            if (days >= 5) {
                inc.autoSysFields(false);
                inc.state = 7; // State 7: Closed
                inc.incident_state = 7;
                inc.active = false;
                inc.update();
            }
        }
    } catch (ex) {
        gs.info(ex);
    }
}

function getDateDiffExcWeekends(start, end) {
    var days = 0;
    while (start < end) {
        start.addDaysUTC(1);

        if (start.getDayOfWeekUTC() != 6 && start.getDayOfWeekUTC() != 7) {
            days++;
        }
    }
    return days;
}
 
but after the incident is closed my name is getting updated on the incident 
TarunChowdary_0-1744910395401.png

 

 Is it possible to change the name to system instead of my name?
 
6 REPLIES 6

@TarunChowdary , @Edxavier Robert is correct. I have added the field to my form view, or you can do it from a list view, too.

muhammadosama55
Tera Contributor

Yes, it's possible to prevent your name from being updated and instead set the sys_updated_by field (which tracks the user who last updated the record) to "system" or some other user, like admin, when updating the record.

// Set sys_updated_by to "system" or another user
inc.sys_updated_by = 'system';

// Or replace 'system' with any other user (e.g., 'admin')