Catalog item date variable.

abhisek
Tera Contributor

Hi All,

I am working on a requirement.

I have 3 catalog variables 'Policy', 'Date' and 'exemption'. 'Policy' refers to a custom table 'abc' which has a column 'end date' and 'name'. 'Date' is a date variable and 'exemption' is a multiple-choice variable.

The requirement is: (1) If the value of  'exemption' is neither and the record is selected for the variable 'Policy' has 300 days as 'end date' value then the value of 'Date' variable should auto set to the date which is current date + 300 days and  'Date' variable should not be read only, user must be able to select any date in the future but not more than current date + 300 days.

(2) If the value of 'exemption' is neither and the record is selected for the variable 'Policy' has no values for 'end date' (means 'end date' value is not mentioned) then 'Date' variable should not be read only, user must be able to select any date in the future with no restrictions.

 

I have tried this with script include and catalog client script but not working.

Can you please help me out with correct approach.

Thanks in advance.

 

Regards,

Abhisek Chattaraj.

1 ACCEPTED SOLUTION

@abhisek  

Your script include is not client callable

try this

Script Include: client callable

var GetDefaultExpiration = Class.create();
GetDefaultExpiration.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getExpirationDate: function() {
        var policy = this.getParameter('sysparm_sys_id');
        var expiration = '';
        // Check if policyID is valid
        if (policy) {
            var policyGR = new GlideRecord('abc');
            if (policyGR.get(policy)) {
                expiration = policyGR.end_date;
            }
        }
        return expiration;
    },

    type: 'GetDefaultExpiration'
});

AnkurBawiskar_0-1735645907648.png

Then onChange catalog client script on Policy variable

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var policySysId = g_form.getValue('policy');
    var exemptionValue = g_form.getValue('exemption'); // give correct variable name here

    if (exemptionValue === 'neither') { // give correct choice value here
        var ga = new GlideAjax('GetDefaultExpiration');
        ga.addParam('sysparm_name', 'getExpirationDate');
        ga.addParam('sysparm_sys_id', policySysId);
        ga.getXMLAnswer(function(response) {
            var endDate = response;
            if (endDate) {
                var endDateMs = new Date(endDate).getTime();
                var currentDateMs = new Date().getTime();
                var maxDateMs = currentDateMs + (300 * 24 * 60 * 60 * 1000); // 300 days in milliseconds

                if (endDateMs === maxDateMs) {
                    var maxDate = new Date(maxDateMs);
                    g_form.setValue('date', maxDate.toISOString().split('T')[0]);
                    g_form.setReadOnly('date', false);
                } else {
                    g_form.setReadOnly('date', false);
                }
            } else {
                g_form.setReadOnly('date', false);
            }
        });
    }
}

Then you need to have 1 more onSubmit catalog client script

function onSubmit() {
    var dateValue = g_form.getValue('date');
    var exemptionValue = g_form.getValue('exemption');

    if (exemptionValue === 'neither') {
        var selectedDateMs = new Date(dateValue).getTime();
        var currentDateMs = new Date().getTime();
        var maxDateMs = currentDateMs + (300 * 24 * 60 * 60 * 1000); // 300 days in milliseconds

        if (selectedDateMs > maxDateMs) {
            alert('The selected date cannot be more than 300 days from today.');
            return false;
        }
    }
    return true;
}

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

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

@abhisek  

without sharing the scripts you tried, we can't help.

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

Hi @Ankur Bawiskar 

Script include is:

var GetDefaultExpiration = Class.create();
GetDefaultExpiration.prototype = {
initialize: function() {
},

getExpirationDate: function() {
var policy = this.getParameter('sys_id');
var expiration = null;

// Check if policyID is valid
if (policy) {
var policyGR = new GlideRecord('abc');
if (policyGR.get(policy)) {
expiration = policyGR.end_date;
}
}


return expiration || '';
},

type: 'GetDefaultExpiration'
};

on change catalog client script is:

 case 'neither':
          
          var policyID = g_form.getValue('policy'); 
 
            if (!policyID) {
                return; 
            }
 
            var ga = new GlideAjax('GetDefaultExpiration'); 
            ga.addParam('sys_method', 'getExpirationDate'); 
            ga.addParam('sys_id', policyID); 
 
            
            ga.getXMLAnswer(function(response) {
                var expirationValue = response.responseXML.documentElement.getAttribute('answer');
                
                
                if (expirationValue) {
                    var expirationDate;
                    expirationValue = parseInt(expirationValue, 10);
                    expirationDate = new Date(today);
                    expirationDate.setDate(today.getDate() + expirationValue); 
                    var formattedDate = expirationDate.toISOString().split('T')[0]; 
                    g_form.setReadOnly(expirationField, false);
                    g_form.setValue(expirationField, formattedDate); 
                } 
                   
                }
            });
            break;
 
}
}
 

@abhisek  

Your script include is not client callable

try this

Script Include: client callable

var GetDefaultExpiration = Class.create();
GetDefaultExpiration.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getExpirationDate: function() {
        var policy = this.getParameter('sysparm_sys_id');
        var expiration = '';
        // Check if policyID is valid
        if (policy) {
            var policyGR = new GlideRecord('abc');
            if (policyGR.get(policy)) {
                expiration = policyGR.end_date;
            }
        }
        return expiration;
    },

    type: 'GetDefaultExpiration'
});

AnkurBawiskar_0-1735645907648.png

Then onChange catalog client script on Policy variable

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var policySysId = g_form.getValue('policy');
    var exemptionValue = g_form.getValue('exemption'); // give correct variable name here

    if (exemptionValue === 'neither') { // give correct choice value here
        var ga = new GlideAjax('GetDefaultExpiration');
        ga.addParam('sysparm_name', 'getExpirationDate');
        ga.addParam('sysparm_sys_id', policySysId);
        ga.getXMLAnswer(function(response) {
            var endDate = response;
            if (endDate) {
                var endDateMs = new Date(endDate).getTime();
                var currentDateMs = new Date().getTime();
                var maxDateMs = currentDateMs + (300 * 24 * 60 * 60 * 1000); // 300 days in milliseconds

                if (endDateMs === maxDateMs) {
                    var maxDate = new Date(maxDateMs);
                    g_form.setValue('date', maxDate.toISOString().split('T')[0]);
                    g_form.setReadOnly('date', false);
                } else {
                    g_form.setReadOnly('date', false);
                }
            } else {
                g_form.setReadOnly('date', false);
            }
        });
    }
}

Then you need to have 1 more onSubmit catalog client script

function onSubmit() {
    var dateValue = g_form.getValue('date');
    var exemptionValue = g_form.getValue('exemption');

    if (exemptionValue === 'neither') {
        var selectedDateMs = new Date(dateValue).getTime();
        var currentDateMs = new Date().getTime();
        var maxDateMs = currentDateMs + (300 * 24 * 60 * 60 * 1000); // 300 days in milliseconds

        if (selectedDateMs > maxDateMs) {
            alert('The selected date cannot be more than 300 days from today.');
            return false;
        }
    }
    return true;
}

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

Hi @Ankur 

Thanks a lot for your help and time.

end date can be anything like 300 days or 400 days. It is dynamic.

Regards,

Abhisek Chattaraj.