- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I want to add an if condition in my existing variable set Client Script so it does not affect other catalog items. I have written the below code to clear out the values if poet type is not capital but it is not working(highlighted in red color)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
To prevent a Variable Set Client Script from affecting other Catalog Items, the most reliable method is to check the sys_id of the current Catalog Item using g_form.getUniqueValue().
The issue in your current script is likely the use of g_form.getParameter('sysparam_id'), which often returns the sys_id of the Variable Set or nothing at all, rather than the parent Catalog Item.
Please try below logic if its works.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
if (newValue === '') {
g_form.setValue('owner', '');
g_form.setValue('poet_type', '');
g_form.setValue('poet_company', '');
g_form.hideFieldMsg('captial_poet_number', true);
}
return;
}
// 1. Formatting Logic
var formattedValue = newValue.replace(/\s/g, '');
if (newValue !== formattedValue) {
g_form.setValue('captial_poet_number', formattedValue);
return;
}
// 2. Format Validation
var formatFilter = /^[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}$/;
if (!newValue.match(formatFilter)) {
g_form.setValue('captial_poet_number', '');
g_form.showFieldMsg('captial_poet_number', 'Please enter a valid POET number format (e.g., XXXXXXXX-XXXX).', 'error');
return;
}
// 3. Server-side Validation
var ga = new GlideAjax('POETValidationAPI');
ga.addParam('sysparm_name', 'validatePOET_ajax');
ga.addParam('sysparm_poet_number', newValue);
ga.getXMLAnswer(function(response) {
var result = JSON.parse(response);
g_form.hideFieldMsg('captial_poet_number', true);
if (!result.isValid) {
// Standard Error Handling
clearPoetFields();
g_form.showFieldMsg('captial_poet_number', result.message.toString(), 'error');
} else {
// Identify the current Catalog Item
var currentItem = g_form.getUniqueValue();
var targetItem = 'a77ad6ec1bb406109b910ed6624bcb49';
// Check if this is the specific item that REQUIRES 'CAPITAL' type
if (currentItem === targetItem && result.poetType !== 'CAPITAL') {
clearPoetFields();
g_form.showFieldMsg('captial_poet_number', 'You must provide a CAPITAL POET in order to process this request.', 'error');
} else {
// Success path for the target item OR standard behavior for all other items
g_form.showFieldMsg('captial_poet_number', result.message.toString(), 'info');
g_form.setValue('owner', result.ownerSysId);
g_form.setValue('poet_type', result.poetType);
g_form.setValue('poet_company', result.poetCompany);
}
}
});
function clearPoetFields() {
g_form.setValue('captial_poet_number', '');
g_form.setValue('owner', '');
g_form.setValue('poet_type', '');
g_form.setValue('poet_company', '');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
grab the URL and search for sysId of your particular catalog item
Then update the logic so that it runs only for your catalog item and not when this script runs on other items
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hope you are doing good.
Did my reply answer your question?
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
It is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @SHALIKAS ,
Why we are creating client script at variable set, if you want separate functionality create separate client scripts at catalog item level. if this variable set is part of that catalog item, you can get all the values of the variables under that.
