Script Include Date Query
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 10:34 PM
Dear all,
I would like to check if the record is exist when end user input with Onchange client script and Script include, below my script, but not working, could anyone help me:
Thank you!
Script Include:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 10:38 PM
Hello @Jeff42 ,
Please give a try to the modified script below and see how it works for you.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Type appropriate comment here, and begin script below
var contractName = g_form.getValue('u_contract_name');
var contractStartDate = g_form.getValue('u_contract_start_date');
g_form.addInfoMessage('Checking for contract name: ' + contractName + ' and start date: ' + contractStartDate);
var ga = new GlideAjax('sn_hr_core.CM_Name_Date_Count');
ga.addParam('sysparm_name', 'checkNameDate');
ga.addParam('u_contract_name', contractName);
ga.addParam('u_contract_start_date', contractStartDate);
ga.getXMLAnswer(function(answer) {
g_form.addInfoMessage('Server response: ' + answer);
if (answer === 'true') {
alert('This contract exists, please adjust');
// Additional logic if needed
// For example, you might want to clear or reset the form fields
g_form.clearValue('u_contract_name');
g_form.clearValue('u_contract_start_date');
}
// Return false to prevent form submission if needed
return false;
});
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 10:51 PM
Thanks Aniket Chavan
not working, the result I want is when user input the same value: Contract Name + Contract Start Date, system will Alert which script include answer is true.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2024 12:47 AM
@Jeff42 Ohh okay then lets try with the code below and see how it works for you.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Type appropriate comment here, and begin script below
var contractName = g_form.getValue('u_contract_name');
var contractStartDate = g_form.getValue('u_contract_start_date');
// Debugging: Add information message to the form
g_form.addInfoMessage('Checking for contract name: ' + contractName + ' and start date: ' + contractStartDate);
var ga = new GlideAjax('sn_hr_core.CM_Name_Date_Count');
ga.addParam('sysparm_name', 'checkNameDate');
ga.addParam('u_contract_name', contractName);
ga.addParam('u_contract_start_date', contractStartDate);
ga.getXMLAnswer(function(answer) {
// Debugging: Add information message to the form
g_form.addInfoMessage('Server response: ' + answer);
if (answer == 'true') {
alert('This contract exists, please adjust');
// Additional logic if needed
// For example, you might want to clear or reset the form fields
g_form.clearValue('u_contract_name');
g_form.clearValue('u_contract_start_date');
} else {
// If the response is 'false' or anything other than 'true'
// Add any additional logic here if needed
}
// Return false to prevent form submission if needed
return false;
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 10:45 PM
The GlideAjax callback function is asynchronous, so returning false at the end of the onChange function won't prevent the form submission. Instead, you should use the callback function to set a variable indicating whether the contract exists or not and then use that variable to decide whether to allow the form submission.
Here's an updated version of your client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Type appropriate comment here, and begin script below
var contractName = g_form.getValue('u_contract_name');
var contractStartDate = g_form.getValue('u_contract_start_date');
var ga = new GlideAjax('sn_hr_core.CM_Name_Date_Count');
ga.addParam('sysparm_name', 'checkNameDate');
ga.addParam('u_contract_name', contractName);
ga.addParam('u_contract_start_date', contractStartDate);
ga.getXMLAnswer(function (answer) {
if (answer == 'true') {
alert('This contract is already exist, please adjust');
// Set a variable indicating that the contract exists
g_form.setValue('u_contract_already_exists', true);
} else {
// Set a variable indicating that the contract doesn't exist
g_form.setValue('u_contract_already_exists', false);
}
// Trigger a submission only if the contract doesn't exist
if (!g_form.getValue('u_contract_already_exists')) {
onSubmit();
}
});
// Returning false here won't prevent form submission,
// instead, use the callback function to decide whether to submit the form
}
Kindly mark helpful/accepted if it helps you.
Thanks