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

Brad Bowman
Mega Patron

Good job with the logging to start.  You are attempting a string method toLowerCase() on an array, which is not a string.  You can either add a .join(',') before the toLowerCase if you just need to test that the category value is in the array, or maintain the array by executing a for loop on it, inside of which you can convert the value of the member to lowercase to compare to the lowercase category. 

var vrCategories = [
            'vulnerability-UEUDCS',
            'vulnerability-UDCSS',
            'vulnerability-UISS',
            'vulnerability-UDNS',
            'vulnerability-Undefined'
        ];

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

return assignmentGroupSysId;

Hi Brad,

 

I inserted the for loop and the vrCategories is still undefined in the logs. Do I need to get the values defined in the client script?

 

Brett21_0-1769621012003.png

 

Yes, sorry, I just posted your array assignment to the return.  Re-post your full script if it's still not working and we'll see what's next. 

Hi Brad,

 

Reposting script include yeah still undefined.

 

 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('Before For LOOP');
        gs.info('VR Categories: ' + vrCategories[i].toString().toLowerCase().indexOf(category.toLowerCase()));
        gs.info('VR subcat: ' + subcategory.toLowerCase());

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

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


        return assignmentGroupSysId;
    },

    type: 'VRAssignmentGroupLookup'