How to write the script

PRAGHATIESH S
Tera Expert

Hi,

I have created one decision builder with input is "company" table reference and output is "x" or "Y". Similarly I created one variable "Company name" on record producer reference to the company table.

 

Now I need to get the value of " company name" variable and check the decision builder output. If output is "x", need to resolve the case.

 

How to achieve this scenario, can someone tell me.

2 REPLIES 2

Collin Romeijn
Mega Guru

Hi Praghatiesh S,

 

to use a decision table trough scripts use the decision api:
https://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_referenc...

Search for the part with: DecisionTableAPI - getDecision(String decisionID, Map inputs).

In the example part is a good way to fill in the input en then use the output.

Kind Regards,

Collin R.

Community Alums
Not applicable

Hi @PRAGHATIESH S ,

 

You need to create a script include to call from the onSubmit() Client script of the record producer.

Script Include would look like-

var CaseResolutionScript = Class.create();
CaseResolutionScript.prototype = {
    initialize: function() {
    },
    checkDecisionAndResolve: function(recordProducerId) {
        var rp = new GlideRecord('sc_cat_item_producer');
        if (rp.get(recordProducerId)) {
            var companyName = rp.variables.company_name;
            var decisionTableId = 'your_decision_table_sys_id'; //decision table sys_id
            var decisionInput = {
                'company': companyName
            };
            var decisionAPI = new sn_dt.DecisionTableAPI();
            var decisionResult = decisionAPI.getDecision(decisionTableId, decisionInput);
            // Check the decision result and perform accordingly
            if (decisionResult && decisionResult.decision == 'x') {
                var caseRecord = new GlideRecord('sn_customerservice_case');
                caseRecord.get(rp.target_table);
                caseRecord.state = 'resolved'; // set a valid state
                caseRecord.update();
            }
        }
    },
    type: 'CaseResolutionScript'
};

 

onSubmit Client Script-

(function producerScript(current) {
    var caseResolution = new CaseResolutionScript();
    caseResolution.checkDecisionAndResolve(current.sys_id);
})(current);

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!


Thanks & Regards,

Sanjay Kumar