We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Glide Ajax script to return group to populate Incident based off cat/sub cat

Brett21
Tera Guru

Hi community,

 

Working on this Script Include and Client script that will auto populate the assignment group based off cat/subcat.  However my assignment group isn't being returned.  My log statements show that my category array is return undefined. Is my syntax correct or is there a better way to do this in a Script include?

 

SCRIPT INCLUDE:

 
var VRAssignmentGroupLookup = Class.create();
VRAssignmentGroupLookup.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getAssignmentGroup: function() {
        var category = this.getParameter('sysparm_category');
        var subcategory = this.getParameter('sysparm_subcategory');
        var assignmentGroupSysId = '';

        gs.info('Starting with Array Logic');
		gs.info('Cat/Subcat: ' + category + ':' + subcategory);

        // 1. Logic for Vulnerability Remediation
        var vrCategories = [
            'vulnerability-UEUDCS',
            'vulnerability-UDCSS',
            'vulnerability-UISS',
            'vulnerability-UDNS',
            'vulnerability-Undefined'
        ];

		gs.info('VR Categories: '+ vrCategories.toLowerCase().indexOf(category));
		gs.info('VR subcat: ' + subcategory.toLowerCase());

        if (vrCategories.toLowerCase().indexOf(category) >= 0 && subcategory.toLowerCase() === 'vulnerability remediation' ) {
            assignmentGroupSysId = 'b87934532598ca904db4d91bf5d6c912';
        }
        gs.info('Array Category1: ' + vrCategories);
        gs.info('Incident Category: ' + category);
        gs.info('Array Category2: ' + vrCategories);
        gs.info('CAT Assignment Group: ' + assignmentGroupSysId);


        return assignmentGroupSysId;
    },

    type: 'VRAssignmentGroupLookup'
});

 

CLIENT SCRIPT:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var ga = new GlideAjax('VRAssignmentGroupLookup');
    ga.addParam('sysparm_name', 'getAssignmentGroup');
    ga.addParam('sysparm_category', g_form.getValue('category'));
    ga.addParam('sysparm_subcategory', g_form.getValue('subcategory'));
    ga.getXMLAnswer(function(answer) {
        if (answer) {
            g_form.setValue('assignment_group', answer);
			alert('answer');
        }
    });

}
1 ACCEPTED SOLUTION

I just noticed the triple =.  I've had trouble in some case when checking the typeOf a script variable that was assigned from a client, so I'd go with == and change the logging a bit to provide better feedback, and to make sure there's not a trailing space at the end of the subcategory name:

 var VRAssignmentGroupLookup = Class.create();
 VRAssignmentGroupLookup.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getAssignmentGroup: function() {
        var category = this.getParameter('sysparm_category');
        var subcategory = this.getParameter('sysparm_subcategory');
        var assignmentGroupSysId = '';

        gs.info('Cat/Subcat: ' + category + ':' + subcategory + ':');

        // 1. Logic for Vulnerability Remediation
        var vrCategories = [
            'vulnerability-UEUDCS',
            'vulnerability-UDCSS',
            'vulnerability-UISS',
            'vulnerability-UDNS',
            'vulnerability-Undefined'
        ];

        gs.info('VR subcat: ' + subcategory.toLowerCase() + ':');

        for (var i = 0; i < vrCategories.length; i++) {
			gs.info('VR Categories: ' + vrCategories[i] + ' :' + vrCategories[i].toString().toLowerCase().indexOf(category.toLowerCase()));
            if (vrCategories[i].toString().toLowerCase().indexOf(category.toLowerCase()) >= 0 && subcategory.toLowerCase() == 'vulnerability remediation') {
				gs.info('inside if');
                assignmentGroupSysId = 'b87934532598ca904db4d91bf5d6c912';
            }
        }

        gs.info('CAT Assignment Group: ' + assignmentGroupSysId);

        return assignmentGroupSysId;
    },

    type: 'VRAssignmentGroupLookup'
 });

 

View solution in original post

9 REPLIES 9

The log after 'Before For LOOP' doesn't make sense and will cause an error as i is not yet defined.  Put that line as is inside the for loop to see if the value is >= 0.  What are the values of category and subcategory showing from the second log line?

Hi Brad,

Moved the logging and the vrCategory and I get back a 0 for one of the vrCategories. The assignment group is still empty and doesn't auto populate.  Pasted the logs

 

 Brett21_0-1769632255277.png

 

 

I just noticed the triple =.  I've had trouble in some case when checking the typeOf a script variable that was assigned from a client, so I'd go with == and change the logging a bit to provide better feedback, and to make sure there's not a trailing space at the end of the subcategory name:

 var VRAssignmentGroupLookup = Class.create();
 VRAssignmentGroupLookup.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getAssignmentGroup: function() {
        var category = this.getParameter('sysparm_category');
        var subcategory = this.getParameter('sysparm_subcategory');
        var assignmentGroupSysId = '';

        gs.info('Cat/Subcat: ' + category + ':' + subcategory + ':');

        // 1. Logic for Vulnerability Remediation
        var vrCategories = [
            'vulnerability-UEUDCS',
            'vulnerability-UDCSS',
            'vulnerability-UISS',
            'vulnerability-UDNS',
            'vulnerability-Undefined'
        ];

        gs.info('VR subcat: ' + subcategory.toLowerCase() + ':');

        for (var i = 0; i < vrCategories.length; i++) {
			gs.info('VR Categories: ' + vrCategories[i] + ' :' + vrCategories[i].toString().toLowerCase().indexOf(category.toLowerCase()));
            if (vrCategories[i].toString().toLowerCase().indexOf(category.toLowerCase()) >= 0 && subcategory.toLowerCase() == 'vulnerability remediation') {
				gs.info('inside if');
                assignmentGroupSysId = 'b87934532598ca904db4d91bf5d6c912';
            }
        }

        gs.info('CAT Assignment Group: ' + assignmentGroupSysId);

        return assignmentGroupSysId;
    },

    type: 'VRAssignmentGroupLookup'
 });

 

Hi Brad,

 

Thank you that was it.  It is working as expected now.

SumanthDosapati
Mega Sage

@Brett21 

@Brett21 

I guess stop converting into lower case in below line and in addition to Brad's suggestion should solve your issue

gs.info('VR Categories: '+ vrCategories.toLowerCase().indexOf(category));