- 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
Hi @SHALIKAS,
When you work in portal you can get sysparm_id throught out URL, so if you want to use this script both side client&portal, you can consider this script below:
if(window != null){
// native
item = g_form.getParameter('sysparm_id');
}
else{
// portal
var url = top.location.href;
item = new URLSearchParams(url).get("sys_id");
}
If my response helped, please mark it as correct and close the thread - this help future readers find the solution faster!
Regards,
Duy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
It is not working, not sure if it is because of GlideAjax or getXMLAnswer function that it is not going in the if condition
- 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', '');
}
}
