Scheduled Script Execution - Certain Records not appearing

JayAdmin_16
Mega Sage

Hello! I'm still quite new to ServiceNow and hoping someone a bit more seasoned in this space can help me. 

Exploring the code below, the intention of this code is to create records on the Patch Roster table (a custom table). When running this script, there’s two “types” of Patching Roster Records – Quarterly and Monthly.

When this script is run, quarterly records do appear in the Patching Roster Table. However, monthly does not.

I didn’t write this code below, but from my understanding there isn’t anything in the below script that should prevent monthly scripts from appearing? 

deleteRoster();
createRoster();

function createRoster() {

    var serviceName = "";
    var serviceClassification = "";
    var businessCriticallity = "";
    var serviceOwner = "";
    var serviceManager = "";
    var supportGroup = "";
    var UsedFor = "";
    var ManagingDepartment = "";
	var PatchGroupType = ""; 

    var getServer = new GlideRecord("cmdb_ci_server");
    getServer.addEncodedQuery("u_patch_group_reference!=NULL^install_status!=7");
    //getServer.addEncodedQuery("sys_class_name!=cmdb_ci_win_server");
    //getServer.setLimit(5);
    getServer.query();

    while (getServer.next()) {
        UsedFor = getServer.used_for;


        var checkKeyValue = new GlideRecord("cmdb_key_value");
        checkKeyValue.addEncodedQuery("configuration_item=" + getServer.sys_id + "^key=Service");
        checkKeyValue.query();

        while (checkKeyValue.next()) {


            var getService = new GlideRecord("cmdb_ci_service");


            getService.addEncodedQuery("name=" + checkKeyValue.value);
            getService.query();

            if (getService.next()) {
                serviceName = getService.name;
                serviceClassification = getService.service_classification.getDisplayValue();
                businessCriticallity = getService.busines_criticality;
                serviceOwner = getService.owned_by.name;
                serviceManager = getService.managed_by.name;
                supportGroup = getService.support_group.name;
                ManagingDepartment = getService.u_managing_department;
            }
            var getPatchSchedule = new GlideRecord("u_patch_schedule");
            getPatchSchedule.addEncodedQuery("u_patch_name=" + getServer.u_patch_group_reference);
            getPatchSchedule.query();
            while (getPatchSchedule.next()) {
				var patchGroup = new GlideRecord("u_patch_group");
                if (patchGroup.get(getServer.u_patch_group_reference)) {
                    PatchGroupType = patchGroup.u_type;
                }
                var checkRecord = new GlideRecord("u_patch_roster");
                checkRecord.initialize();
                checkRecord.u_server_name = getServer.name;
                checkRecord.u_service_name = serviceName;
                checkRecord.u_patch_group = getServer.u_patch_group_reference;
                checkRecord.u_patch_schedule_name = getPatchSchedule.u_patch_name;
                checkRecord.u_service_classification = serviceClassification;
                checkRecord.u_business_criticality = businessCriticallity;
                checkRecord.u_start_time = getServer.u_patch_group_reference.u_start_time.getDisplayValue();
                checkRecord.u_end_time = getServer.u_patch_group_reference.u_end_time.getDisplayValue();
                checkRecord.u_date = getPatchSchedule.u_date;
                checkRecord.u_quarter = getPatchSchedule.u_quarter_choice;
                checkRecord.u_year = getPatchSchedule.u_year;
                checkRecord.u_status = getServer.install_status.getDisplayValue();
                checkRecord.u_service_owner = serviceOwner;
                checkRecord.u_service_manager = serviceManager;
                checkRecord.u_support_group = supportGroup;
                checkRecord.u_class = getServer.sys_class_name;
                checkRecord.u_used_for = UsedFor;
                checkRecord.u_managing_department = ManagingDepartment;
				checkRecord.u_type = PatchGroupType;
                checkRecord.insert();
            }
        }
    }
}

function deleteRoster() {
    var deleteRoster = new GlideRecord("u_patch_roster");
    deleteRoster.query();
    while (deleteRoster.next()) {
        deleteRoster.deleteRecord();
    }
}


Let me know if further explanation is required. Any advice and gentle guidance would be greatly appreciated,  

1 REPLY 1

Mark Manders
Mega Patron

We don't know the data in your custom table. I see that you are setting 'u_quarter' and 'u_year', but I don't see 'u_month'. It is really depending on how your custom table is setup. You are setting the 'quarter', so I assume that is what you are looking at for 'quarter' records, but what about 'month'? How do you recognize those? 

And it can very well be that the record isn't inserted, because there is no 'quarter' available for those records, in which case you should put some 'if/else's' in there.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark