We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Add condition on script include

tindiz
Giga Guru

Hi Experts, I need help in adding this condition to my script include -

 

var TimeSheetExceptionGenerator = Class.create();
TimeSheetExceptionGenerator.prototype = {
    initialize: function() {
        this.timeCardUserRoleId = TimeCardUtil.getTimeCardUserRoleId();
    },

    run: function(date) {
		var company = gs.getProperty('eps.timesheet.company');
        var gr = new GlideRecord('sys_user_has_role');
        gr.addQuery('role', this.timeCardUserRoleId);
        gr.addQuery('state', 'active');
        gr.addQuery('user.active', true);
	gr.addQuery('user.company', company);	
        gr.query();

        var timeCardUsers = [];
        while (gr.next())
            timeCardUsers.push(gr.getValue('user'));

        this._updateTimeSheetStatusForUser(date, timeCardUsers);
    },

    _updateTimeSheetStatusForUser: function(date, users) {
        if (JSUtil.notNil(users) && users.length > 0) {
            users.forEach(function(user) {
                user = user.toString();
                var weekStartsOn = TimeCardUtil.getStartOfWeekByDate(date, user);
                var timeSheet = new GlideRecord('time_sheet');
                timeSheet.addQuery('week_starts_on', weekStartsOn);
                timeSheet.addQuery('user', user);
                timeSheet.query();

                var state = 'Not_Submitted';
                if (timeSheet.next()) {
                    state = timeSheet.getValue('state');
                }

                this.createOrUpdateExceptionRecord(user, weekStartsOn, state);
            }, this);
        }
    },
    
    createOrUpdateExceptionRecord: function(user, weekStartsOn, state) {
        var gr = new GlideRecord('time_sheet_exception');
        gr.addQuery('user', user);
        gr.addQuery('week_starts_on', weekStartsOn);
        gr.query();
        if (gr.next()) {
            if (gr.getValue('state') != state) {
                gr.setValue('state', state);
                gr.update();
            }
        } else {
            var domainId = TimeCardUtil.getUserDomainId(user);
            gr = new GlideRecord('time_sheet_exception');
            gr.setValue('user', user);
            gr.setValue('week_starts_on', weekStartsOn);
            gr.setValue('state', state);
            if (JSUtil.notNil(domainId))
                gr.setValue('sys_domain', domainId);
            gr.insert();
        }
    },

    type: 'TimeSheetExceptionGenerator'
};

 

The condition that I need to add is active=true^correlation_id=Client Director

However, I don't seem to know where to add it on the script. Please help. Thanks! 

2 REPLIES 2

Runjay Patel
Giga Sage

Hi @tindiz ,

 

you need to add in below function. check the revised code.

run: function(date) {
		var company = gs.getProperty('eps.timesheet.company');
        var gr = new GlideRecord('sys_user_has_role');
        gr.addQuery('role', this.timeCardUserRoleId);
        gr.addQuery('state', 'active');
        gr.addQuery('user.active', true);
gr.addEncodedQuery('user.active=true^user.correlation_id=Client Director');
	gr.addQuery('user.company', company);	
        gr.query();

        var timeCardUsers = [];
        while (gr.next())
            timeCardUsers.push(gr.getValue('user'));

        this._updateTimeSheetStatusForUser(date, timeCardUsers);
    },

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Ankur Bawiskar
Tera Patron

@tindiz 

how are you calling it?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader