Make changes in variable set client script so it does not affect other catalog items

SHALIKAS
Tera Guru

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)

 

function onChange(control, oldValue, newValue, isLoading) {


    if (newValue) {
        //newValue = newValue.trim();
        newValue = newValue.replace(/\s/g, '');
        if (newValue !== g_form.getValue('captial_poet_number')) {
            g_form.setValue('captial_poet_number', newValue);
            return;
        }
    }

    if (newValue === '') {
        g_form.setValue('owner', '');
        g_form.setValue('poet_type', '');
        g_form.setValue('poet_company', '');
        g_form.hideFieldMsg('captial_poet_number', true);
        g_form.setDisplay('poet_error_message', false);

        return;
    }
    if (isLoading) {
        g_form.hideFieldMsg('captial_poet_number', true);
        g_form.setDisplay('poet_error_message', false);

    }

    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');
        g_form.setValue('owner', '');
        g_form.setValue('poet_type', '');
        g_form.setValue('poet_company', '');
        //g_form.clearValue('validate_poet_number');
        return;
    }

    if (g_form.getValue('validate_poet_number') == true) {
       
        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) {
                g_form.setValue('captial_poet_number', '');
                g_form.showFieldMsg('captial_poet_number', result.message.toString(), 'error');

                g_form.setValue('owner', '');
                g_form.setValue('poet_type', '');
                g_form.setValue('poet_company', '');

                g_form.setValue('validate_poet_number', result.isValidPoet);


            } else {
                    var sysID=g_form.getParameter('sysparam_id');
                    if(sysID=='a77ad6ec1bb406109b910ed6624bcb49'){
                       
                    if (result.poetType != 'CAPITAL') {
                        g_form.setValue('captial_poet_number', '');
                        g_form.setValue('poet_error_message', false);
                        g_form.setValue('validate_poet_number', '');
                        g_form.setValue('owner', '');
                        g_form.setValue('poet_company', '');
                        g_form.setValue('poet_type', '');

                        g_form.showFieldMsg('captial_poet_number', 'You must provide a CAPITAL POET in order to process this request.');
                    } else {
                       
                        g_form.showFieldMsg('captial_poet_number', result.message.toString(), 'info', true);

                        g_form.setValue('owner', result.ownerSysId);

                        g_form.setValue('poet_type', result.poetType);

                        g_form.setValue('poet_company', result.poetCompany);

                    }
                }
               
               
                g_form.showFieldMsg('captial_poet_number', result.message.toString(), 'info', true);

                g_form.setValue('owner', result.ownerSysId);

                g_form.setValue('poet_type', result.poetType);

                g_form.setValue('poet_company', result.poetCompany);

            }

        });
    }
}
1 ACCEPTED SOLUTION

suraj sengar
Giga Guru

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', '');
}
}

View solution in original post

7 REPLIES 7

DuyPham
Tera Contributor

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

It is not working, not sure if it is because of GlideAjax or getXMLAnswer function that it is not going in the if condition

suraj sengar
Giga Guru

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', '');
}
}