How to restrict client script to make changes on the records created before a particular date

VIKAS MISHRA
Tera Contributor

I have written one on change client script that actually works on field "caller" on incident if we change the caller then it will auto populate the appropriate CI on the bases of that, but we do not want this CI to be auto populated on the incident which has been created before 1st Aug 2023.

 

Below is the client scrupt

 

function onChange(control, oldValue, newValue, isLoading) {
    //if (isLoading) {
    //    return;
    //}

    var created = g_form.getValue('sys_created_on');
    if(created ) {

    }
    if (newValue == '') {
        g_form.setVisible('cmdb_ci', true);
        g_form.clearValue('cmdb_ci');
        return;
    }
    var caller = g_form.getValue('caller_id');
    var ga = new GlideAjax('Inc_ci');
    ga.addParam('sysparm_name', "getCIList");
    ga.addParam('sysparm_role', caller);
    ga.getXMLAnswer(function(answer) {
        //g_form.addInfoMessage('Answer '+answer);
        if (answer != '' && answer != 'List') { // Ci available - only 1
            g_form.setDisplay('cmdb_ci', true);
            g_form.setValue('cmdb_ci', answer);
        } else if (answer == 'List') { // Ci available - multiple
           g_form.clearValue('cmdb_ci');
            g_form.setVisible('cmdb_ci', true);
        } else if (answer == '') { // Ci avaialble - No
            g_form.clearValue('cmdb_ci');
        }
    });
}
7 REPLIES 7

It would always be 1 st august 2023, 

Actually my client script is getting used to populate the CI field's value on the bases of caller, but we do not want this to be auto populated for the tickets which was already created before 1st of aug, becuase old ticket already have its value saved in field "configuration item:

@VIKAS MISHRA 

then use this

Script Include:

var Inc_ci = Class.create();
Inc_ci.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getCIList: function() {

		var myDateTime = new GlideDateTime();
		myDateTime.setValue('2023-08-01 00:00:00');
		var user_id = this.getParameter('sysparm_role');
		var ci = new GlideRecord('cmdb_ci_pc_hardware');
		ci.addQuery('assigned_to', user_id);
		ci.query();
		if(ci.next()){
			var createdDateTime = new GlideDateTime(ci.sys_created_on);
			if(createdDateTime.before(myDateTime))
				return 'false';
			else
				return ci.getUniqueValue();
		}
	},
	type: 'Inc_ci'
});

Now based on the returned value check if it's false; if yes then don't set the value.

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

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

@VIKAS MISHRA 

Hope you are doing good.

Did my reply answer your question?

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

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