- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 06:58 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2024 03:55 AM
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'
});
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2024 02:19 AM
without sharing the scripts you tried, we can't help.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2024 03:48 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2024 03:55 AM
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'
});
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2024 04:26 AM
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.