Script include for approvers

Russell Abbott
Kilo Sage

I have a reference variable that looks at the cost center table (cmn_cost_center)

I'd like to reference a script include that will look at a custom table containing cost center approvers (x_cost_center_approvers) and only show in that reference field a list of all cost centers that actually have approvers

I can't seem to get the lookup to work, instead it shows me all cost centers still

 

If anyone can help guide me that would be great!

 

Here's the reference qualifier call

 

 

 

javascript: new global.CostCenterApproverChecker().checkApproverExists(current.variables.name);

 

 

 

 

Here's the script include

Name: CostCenterApproverChecker

API: global.CostCenterApproverChecker

Client Callable: True

All Application Scopes

 

 

 

var CostCenterApproverChecker = Class.create();
CostCenterApproverChecker.prototype = {
    initialize: function() {},

    checkApproverExists: function(name) {
        var approverGR = new GlideRecord('x_ohs_och_approver_cost_center_approvers');
        approverGR.addQuery('cost_center', name);
        approverGR.setLimit(1);
        approverGR.query();

        return approverGR.hasNext();
    },

    type: 'CostCenterApproverChecker'
};

 

 

 

 

3 REPLIES 3

Amit Verma
Kilo Patron
Kilo Patron

Hi @Russell Abbott 

 

Can you please disable Client Callable checkbox and retry. The Script Include you have shared is not a client callable one.

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

It needs to be called in a variable on a Catalog Item in the Portal so it should be rewritten in a client callable format correct?

 

 

 

 

Thanks!

Russell Abbott
Kilo Sage

Here's how I fixed this and made it work, if anyone stumbles across a similar problem to be solved

Created a script include that is client callable

 

 

var CostCenterApproverChecker = Class.create();
CostCenterApproverChecker.prototype = {
    initialize: function() {},

    checkApproverExists: function() {
        var cost_center_number = this.getParameter('sysparm_cost_center_number');
        var result = false;
        
        // Query the cost center table
        var costCenterGR = new GlideRecord('cmn_cost_center'); // Cost center table name
        costCenterGR.addQuery('name', cost_center_number);
        costCenterGR.query();

        if (costCenterGR.next()) {
            var costCenterSysId = costCenterGR.getUniqueValue();
            //gs.info('Found cost center with Sys ID: ' + costCenterSysId);

            // Query the approver table
            var approverGR = new GlideRecord('x_ohs_och_approver_cost_center_approvers');
            approverGR.addQuery('cost_center', costCenterSysId);
            approverGR.setLimit(1);
            approverGR.query();

            if (approverGR.next()) {
                result = true;
                //gs.info('Approver exists for cost center Sys ID: ' + costCenterSysId);
            } else {
                //gs.info('No approver found for cost center Sys ID: ' + costCenterSysId);
            }
        } else {
            //gs.info('Cost center not found for name: ' + cost_center_number);
        }

        return result;
    },

    type: 'CostCenterApproverChecker'
};

 

 

  Created an on load client script on the catalog item to populate the required reference field

 

 

function onLoad() {
    // Create a new GlideAjax object
    var ga = new GlideAjax('CostCenterApproverChecker');

    // Add parameters to the GlideAjax object
    ga.addParam('sysparm_name', 'getCostCenterValues');

    // Send the request and handle the response
    ga.getXMLAnswer(function(response) {
        var costCenterValues = response.responseXML.documentElement.getAttribute('answer');
        if (costCenterValues) {
            // Assuming costCenterValues is a comma-separated list of values
            var valuesArray = costCenterValues.split(',');
            // Set the field values
            g_form.setValue('cost_center_number', valuesArray.join(','));
        } else {
            g_form.addErrorMessage('No cost center values found.');
        }
    });
}