- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 07:46 AM
Hi,
There is a catalog item which has 2 fields: tier_1_name and tier_2_name and both are string fields.
Scenario and how the catalog item works:
- The 2 fields above are not displayed on the form at the same time, but are dependant on the other fields: has tier1 onboarding been done? yes/no option.
- If you select yes, then it asks which tier 1 and it gives a list which references the cloud metadata table and displays some records based on tier type and category then it asks for tier_2_name.
- if you select no, the it goes to tier_1_name and no tier_2_name ever displays as tier 1 needs to be done before.
The requirement is that when you are write the tier_1_name, if the record already exists in the cloud metadata table, then you should not be able to do that and the value should clear out and you should get an error saying it already exists. The same thing is also for tier_2_name. however both fields don't exist on the table at the same time and I feel the previous field of which tier 1 (as it references the same cloud matadata table) also complicates things.
Anyways I have made 2 catalog client scripts:
tier_1_name catalog client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('LBGCloudMetadataUtil');
ga.addParam('sysparm_name', 'existsTier1');
ga.addParam('sysparm_tier_1_name', g_form.getValue("tier_1_name"));
ga.getXML(cloudExists);
function cloudExists(response) {
g_form.clearMessages('tier_1_name');
var cloudMetadataExists = response.responseXML.documentElement.getAttribute("answer");
if (cloudMetadataExists=='found') {
g_form.clearValue('tier_1_name');
g_form.showFieldMsg('tier_1_name','This name already exists','error');
}
}
}
tier_2_name catalog client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('LBGCloudMetadataUtil');
ga.addParam('sysparm_name', 'existsTier2');
ga.addParam('sysparm_tier_2_name', g_form.getValue("tier_2_name"));
ga.getXML(cloudExists);
function cloudExists(response) {
g_form.clearMessages('tier_2_name');
var cloudMetadataExists = response.responseXML.documentElement.getAttribute("answer");
if (cloudMetadataExists=='found') {
g_form.clearValue('tier_2_name');
g_form.showFieldMsg('tier_2_name','This name already exists','error');
}
}
}
they are both onChange and they are separated because the variable name is different.
This is the script include I made
var LBGCloudMetadataUtil = Class.create();
LBGCloudMetadataUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//this function will check for name field against the tier 1 variable
existsTier1: function() {
var tier1Name = this.getParameter('sysparm_tier_1_name');
var cloudMetadataGR = new GlideRecord('u_cmdb_ci_cloud_metadata');
cloudMetadataGR.addQuery('name', tier1Name);
cloudMetadataGR.query();
var found = 'not found';
if (cloudMetadataGR.next()) {
found = 'found';
}
return found;
},
//this function will check for name field against the tier 2 variable
existsTier2: function() {
var tier2Name = this.getParameter('sysparm_tier_2_name');
var cloudMetadataGR1 = new GlideRecord('u_cmdb_ci_cloud_metadata');
cloudMetadataGR1.addQuery('name', tier2Name);
cloudMetadataGR1.query();
var found1 = 'not found';
if (cloudMetadataGR1.next()) {
found1 = 'found';
}
return found1;
},
type: 'LBGCloudMetadataUtil'
});
and it all works, it clears out the tier_1_name and tier_2_name fields if they already exist in the cloud metadata table and doesn't clear them out if they don't exist, but is there anyway to do this without making the glideRecord call to the same table twice as its not best practice.
thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 07:50 AM
try this
Script Include: combine the check in single function
var LBGCloudMetadataUtil = Class.create();
LBGCloudMetadataUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkTierNames: function() {
var tier1Name = this.getParameter('sysparm_tier_1_name');
var tier2Name = this.getParameter('sysparm_tier_2_name');
var found = {
tier1: 'not found',
tier2: 'not found'
};
var cloudMetadataGR = new GlideRecord('u_cmdb_ci_cloud_metadata');
if (tier1Name) {
cloudMetadataGR.addQuery('name', tier1Name);
}
if (tier2Name) {
cloudMetadataGR.addQuery('name', tier2Name);
}
cloudMetadataGR.query();
while (cloudMetadataGR.next()) {
if (cloudMetadataGR.name == tier1Name) {
found.tier1 = 'found';
}
if (cloudMetadataGR.name == tier2Name) {
found.tier2 = 'found';
}
}
return JSON.stringify(found);
},
type: 'LBGCloudMetadataUtil'
});
Client Script: tier 1
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('LBGCloudMetadataUtil');
ga.addParam('sysparm_name', 'checkTierNames');
ga.addParam('sysparm_tier_1_name', g_form.getValue("tier_1_name"));
ga.getXMLAnswer(function(response) {
var result = JSON.parse(response);
g_form.clearMessages('tier_1_name');
if (result.tier1 == 'found') {
g_form.clearValue('tier_1_name');
g_form.showFieldMsg('tier_1_name', 'This name already exists', 'error');
}
});
}
tier 2 client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('LBGCloudMetadataUtil');
ga.addParam('sysparm_name', 'checkTierNames');
ga.addParam('sysparm_tier_2_name', g_form.getValue("tier_2_name"));
ga.getXMLAnswer(function(response) {
var result = JSON.parse(response);
g_form.clearMessages('tier_2_name');
if (result.tier2 == 'found') {
g_form.clearValue('tier_2_name');
g_form.showFieldMsg('tier_2_name', 'This name already exists', 'error');
}
});
}
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
03-24-2025 08:02 AM
Hello @snow_beginner ,
Your both the client script and script include function are identical except the tier name. You can use below client scripts and script include example:
Client script for tier1:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('LBGCloudMetadataUtil');
ga.addParam('sysparm_name', 'tierexists');
ga.addParam('sysparm_tier_name', g_form.getValue("tier_1_name"));
ga.getXML(cloudExists);
function cloudExists(response) {
g_form.clearMessages('tier_1_name');
var cloudMetadataExists = response.responseXML.documentElement.getAttribute("answer");
if (cloudMetadataExists=='found') {
g_form.clearValue('tier_1_name');
g_form.showFieldMsg('tier_1_name','This name already exists','error');
}
}
}
Client script tier 2:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('LBGCloudMetadataUtil');
ga.addParam('sysparm_name', 'tierexists');
ga.addParam('sysparm_tier_name', g_form.getValue("tier_2_name"));
ga.getXML(cloudExists);
function cloudExists(response) {
g_form.clearMessages('tier_2_name');
var cloudMetadataExists = response.responseXML.documentElement.getAttribute("answer");
if (cloudMetadataExists=='found') {
g_form.clearValue('tier_2_name');
g_form.showFieldMsg('tier_2_name','This name already exists','error');
}
}
}
Common script include function without two gliderecords:
var LBGCloudMetadataUtil = Class.create();
LBGCloudMetadataUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
tierexists: function() {
var tierName = this.getParameter('sysparm_tier_name');
var cloudMetadataGR = new GlideRecord('u_cmdb_ci_cloud_metadata');
cloudMetadataGR.addQuery('name', tierName );
cloudMetadataGR.query();
var found = 'not found';
if (cloudMetadataGR.next()) {
found = 'found';
}
return found;
},
type: 'LBGCloudMetadataUtil'
});
**** Also I will suggest Instead showing error message, you can write a reference qualifier on tier list, so it will show only those tiers who are not used already.
Kindly mark the answer ✔️ Correct or Helpful ✔️ If it addresses your concern.
Regards,
Siddhesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 07:50 AM
try this
Script Include: combine the check in single function
var LBGCloudMetadataUtil = Class.create();
LBGCloudMetadataUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkTierNames: function() {
var tier1Name = this.getParameter('sysparm_tier_1_name');
var tier2Name = this.getParameter('sysparm_tier_2_name');
var found = {
tier1: 'not found',
tier2: 'not found'
};
var cloudMetadataGR = new GlideRecord('u_cmdb_ci_cloud_metadata');
if (tier1Name) {
cloudMetadataGR.addQuery('name', tier1Name);
}
if (tier2Name) {
cloudMetadataGR.addQuery('name', tier2Name);
}
cloudMetadataGR.query();
while (cloudMetadataGR.next()) {
if (cloudMetadataGR.name == tier1Name) {
found.tier1 = 'found';
}
if (cloudMetadataGR.name == tier2Name) {
found.tier2 = 'found';
}
}
return JSON.stringify(found);
},
type: 'LBGCloudMetadataUtil'
});
Client Script: tier 1
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('LBGCloudMetadataUtil');
ga.addParam('sysparm_name', 'checkTierNames');
ga.addParam('sysparm_tier_1_name', g_form.getValue("tier_1_name"));
ga.getXMLAnswer(function(response) {
var result = JSON.parse(response);
g_form.clearMessages('tier_1_name');
if (result.tier1 == 'found') {
g_form.clearValue('tier_1_name');
g_form.showFieldMsg('tier_1_name', 'This name already exists', 'error');
}
});
}
tier 2 client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('LBGCloudMetadataUtil');
ga.addParam('sysparm_name', 'checkTierNames');
ga.addParam('sysparm_tier_2_name', g_form.getValue("tier_2_name"));
ga.getXMLAnswer(function(response) {
var result = JSON.parse(response);
g_form.clearMessages('tier_2_name');
if (result.tier2 == 'found') {
g_form.clearValue('tier_2_name');
g_form.showFieldMsg('tier_2_name', 'This name already exists', 'error');
}
});
}
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
03-24-2025 08:19 AM
thank you so much!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 08:02 AM
Hello @snow_beginner ,
Your both the client script and script include function are identical except the tier name. You can use below client scripts and script include example:
Client script for tier1:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('LBGCloudMetadataUtil');
ga.addParam('sysparm_name', 'tierexists');
ga.addParam('sysparm_tier_name', g_form.getValue("tier_1_name"));
ga.getXML(cloudExists);
function cloudExists(response) {
g_form.clearMessages('tier_1_name');
var cloudMetadataExists = response.responseXML.documentElement.getAttribute("answer");
if (cloudMetadataExists=='found') {
g_form.clearValue('tier_1_name');
g_form.showFieldMsg('tier_1_name','This name already exists','error');
}
}
}
Client script tier 2:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('LBGCloudMetadataUtil');
ga.addParam('sysparm_name', 'tierexists');
ga.addParam('sysparm_tier_name', g_form.getValue("tier_2_name"));
ga.getXML(cloudExists);
function cloudExists(response) {
g_form.clearMessages('tier_2_name');
var cloudMetadataExists = response.responseXML.documentElement.getAttribute("answer");
if (cloudMetadataExists=='found') {
g_form.clearValue('tier_2_name');
g_form.showFieldMsg('tier_2_name','This name already exists','error');
}
}
}
Common script include function without two gliderecords:
var LBGCloudMetadataUtil = Class.create();
LBGCloudMetadataUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
tierexists: function() {
var tierName = this.getParameter('sysparm_tier_name');
var cloudMetadataGR = new GlideRecord('u_cmdb_ci_cloud_metadata');
cloudMetadataGR.addQuery('name', tierName );
cloudMetadataGR.query();
var found = 'not found';
if (cloudMetadataGR.next()) {
found = 'found';
}
return found;
},
type: 'LBGCloudMetadataUtil'
});
**** Also I will suggest Instead showing error message, you can write a reference qualifier on tier list, so it will show only those tiers who are not used already.
Kindly mark the answer ✔️ Correct or Helpful ✔️ If it addresses your concern.
Regards,
Siddhesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 08:20 AM
@Siddhesh Gawade thanks so much. I have decided to go with the other solution but this one also works.