Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

Issue with service catalog backend list collector populate 3 app but employee center 6 app correctly

aolayemi
Tera Contributor

Please can someone help me with this issue, I created a new field on one of service catalog when user select a particular title, the 6 applications for that title will populate. It worked correctly in the employee center by populating the 6 apps correctly, but backend service catalog the same form is populating only three. I did the configuration by creating client script and script include and decision table. Can anyone help me with this, how I can fix the backend services catalog or better way to achieve this. The field for the apps is list collector. Thanks.

2 REPLIES 2

Ankur Bawiskar
Tera Patron

@aolayemi 

screenshots please along with script

what debugging did you do?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

This is the on change catalog script-

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

    if (isLoading)
        return;

    var appVariable = 'select_the_application_s_required';

    g_form.clearValue(appVariable);

    if (!newValue)
        return;

    var ga = new GlideAjax('GetJobProfileApplications');

    ga.addParam('sysparm_name', 'getApplications');
    ga.addParam('sysparm_job_profile', newValue);

    ga.getXMLAnswer(function(answer) {

        if (answer)
            g_form.setValue(appVariable, answer);

    });
}
 
This is the script include
var GetJobProfileApplications = Class.create();
GetJobProfileApplications.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getApplications: function() {

        try {

            var jobProfile = String(
                this.getParameter('sysparm_job_profile')
            );

            if (!jobProfile)
                return '';

            var decisionId = 'sys id';

            var inputs = {
                u_job_profile: jobProfile
            };

            var dt = new sn_dt.DecisionTableAPI();

            // Find the decision that matches the selected Job Profile
            var decision = dt.getDecision(
                decisionId,
                inputs
            );

            if (!decision || !decision.isValidRecord())
                return '';

            var matchedAnswerSysId =
                String(decision.getUniqueValue());

            // Get the Decision Table answers
            var answers = dt.getAnswers(decisionId);

            if (!answers || !answers.result)
                return '';

            var appString = '';

            // Find the matching answer returned by getDecision()
            for (var i = 0; i < answers.result.length; i++) {

                var row = answers.result[i];

                if (!row.valid ||
                    String(row.value) != matchedAnswerSysId)
                    continue;

                var elements = row.answerElementValues || [];

                for (var j = 0; j < elements.length; j++) {

                    if (elements[j].valid &&
                        elements[j].answerElementName == 'u_application') {

                        appString = String(elements[j].value);
                        break;
                    }
                }

                if (appString)
                    break;
            }

            if (!appString)
                return '';

            var appNames = appString.split(',');
            var sysIds = [];

            for (var x = 0; x < appNames.length; x++) {

                var appName = appNames[x].trim();

                if (!appName)
                    continue;

                var choice = new GlideRecord('sys_choice');

                choice.addQuery('name', 'incident');
                choice.addQuery('element', 'subcategory');
                choice.addQuery('label', appName);
                choice.addQuery('inactive', false);
                choice.setLimit(1);
                choice.query();

                if (choice.next()) {
                    sysIds.push(
                        String(choice.getUniqueValue())
                    );
                }
            }

            return sysIds.join(',');

        } catch (e) {

            gs.error(
                'GetJobProfileApplications: ' + e
            );

            return '';
        }
    },

    type: 'GetJobProfileApplications'
});