scripts to check single line text against table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2024 07:52 PM - edited 02-14-2024 07:53 PM
I have created Single line text in service catalog, and I have table 'trusted domain whitelisting".
Details as per below
Single line Text variable (u_domain_name)
table name = u_trusted_domain_whitelisting
Table Column 1 = u_active (True/False)
Table Column 2 = u_domain_whitelisting
Here is scenario.
Scenario 1
When user enter a name in u_domain_name (variable), need to validate the domain name is existed or not in table "u_trusted_domain_whitelisting" and check if Table Column 1 = u_active (True) and it should check Table Column 2 = u_domain_whitelisting is existed if yes it need to show up an error message "Domain is available" and when user press "OK" it need to clear the u_domain_name (variable).
Scenario 2
When user enter a name in u_domain_name (variable), need to validate the domain name is existed or not in table "u_trusted_domain_whitelisting" and check if Table Column 1 = u_active (False) and it should skip check Table Column 2 = u_domain_whitelisting and nothing happen.
Please help me any one idea about this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2024 08:36 PM
I have put some alert in the code please try let me know alert u getting
function onChange(control, oldValue, newValue, isLoading) {
// Check if the control being changed is 'u_domain_name'
if (control == 'u_domain_name' && !isLoading) {
var domainName = g_form.getValue('u_domain_name');
alert(domainName);
// Check if the entered domain exists in the trusted domain whitelisting table
var domainWhitelisting = new GlideRecord('u_trusted_domain_whitelisting');
domainWhitelisting.addQuery('u_domain_whitelisting', domainName);
domainWhitelisting.query();
if (domainWhitelisting.next()) {
alert("domain exist");
// Domain exists in the table
if (domainWhitelisting.u_active) {
alert("domain exist and active");
// Active domain, show error message
g_form.showFieldMsg('u_domain_name', "Domain is not available.", 'error');
g_form.clearValue('u_domain_name'); // Clear the entered domain
}
// If u_active is false, do nothing
}
// If the domain does not exist in the table, do nothing
}
}
Kindly mark helpful/accepted if it helps you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2024 08:41 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2024 08:54 PM
Seems like we are not getting the data..Can u try using client script and script include, also put one more alert in the beginning inside client script to check if client script triggering or not
function onChange(control, oldValue, newValue, isLoading) {
// Check if the control being changed is 'u_domain_name'
if (control == 'u_domain_name' && !isLoading) {
var domainName = g_form.getValue('u_domain_name');
// Check if the entered domain exists in the trusted domain whitelisting table
var gr = new GlideAjax('CheckDomainAvailabilityAjax');
gr.addParam('sysparm_name', 'checkDomainAvailability');
gr.addParam('sysparm_domain_name', domainName);
gr.getXML(checkDomainCallback);
}
}
function checkDomainCallback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer === 'unavailable') {
g_form.showFieldMsg('u_domain_name', "Domain is not available.", 'error');
g_form.clearValue('u_domain_name'); // Clear the entered domain
}
// If the domain is available, do nothing
}
var CheckDomainAvailabilityAjax = Class.create();
CheckDomainAvailabilityAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkDomainAvailability: function() {
var domainName = this.getParameter('sysparm_domain_name');
// Check if the entered domain exists in the trusted domain whitelisting table
var domainWhitelisting = new GlideRecord('u_trusted_domain_whitelisting');
domainWhitelisting.addQuery('u_domain_whitelisting', domainName);
domainWhitelisting.query();
if (domainWhitelisting.next() && domainWhitelisting.u_active) {
return 'unavailable';
} else {
return 'available';
}
}
});
Kindly mark helpful/accepted if it helps you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2024 09:16 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2024 09:26 PM
Lets debug from the beginning
Just update the client script and see what u getting in the first alert:
function onChange(control, oldValue, newValue, isLoading) {
var domainName = g_form.getValue('u_domain_name');
alert(domainName);
// Check if the entered domain exists in the trusted domain whitelisting table
var gr = new GlideAjax('CheckDomainAvailabilityAjax');
gr.addParam('sysparm_name', 'checkDomainAvailability');
gr.addParam('sysparm_domain_name', domainName);
gr.getXML(checkDomainCallback);
}
}
function checkDomainCallback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer === 'unavailable') {
g_form.showFieldMsg('u_domain_name', "Domain is not available.", 'error');
g_form.clearValue('u_domain_name'); // Clear the entered domain
}
// If the domain is available, do nothing
}