Client callable script include not working

MGR
Tera Contributor

Hi All,

 

Written business rule update incidents which are created from alerts and not having ci need to update incident with CI,

please check the below script for reference.

 

Business rule: asynchronous business rule

script:

    var incidentSysId = current.sys_id;
    new incidentUpdate().getAlertDetails(incidentSysId);
 
script include:
 
getAlertDetails: function(incidentSysId) {
        gs.log('Thursday1' + incidentSysId, 'gov 03-11-2023');

        var alert = new GlideRecord('em_alert');
        alert.addEncodedQuery("incident="+incidentSysId);
        alert.query();
        if (alert.next()) {
            gs.log('Thursday3' + incidentSysId, 'gov 03-11-2023');
            var alertci = alert.getDisplayValue('node');
            var resource = alert.getDisplayValue('resource');
            var nodeStr = alertci.split('.');
            var resourceStr = resource.split('.');
            var nodeStr1 = nodeStr.toString();
            var resourceStr1 = resourceStr.toString();
            var nodeStr2 = nodeStr1[0];
            var resourceStr2 = resourceStr1[0];
        }
        var ci = new GlideRecord('cmdb_ci');
        ci.addEncodedQuery('asset_tagISNOTEMPTY^operational_status=1^u_source=AM');
        ci.addEncodedQuery('nameLIKE' + nodeStr2 + '^ORnameLIKE' + resourceStr2);
        ci.query();
        if (ci.next()) {
            gs.log('Thursday4' + incidentSysId, 'gov 03-11-2023');
            var ciSysId = ci.getValue('sys_id');
        }
        var cmdbci = new GlideRecord('cmdb_rel_ci');
        cmdbci.addEncodedQuery('parent.sys_class_name=cmdb_ci_service');
        cmdbci.addQuery('child', ciSysId);
        cmdbci.query();
        if (cmdbci.next()) {
            gs.log('Thursday5' + incidentSysId, 'gov 03-11-2023');
            var serviceSysId = cmdbci.parent.getValue('sys_id');
        }

        var service = new GlideRecord('cmdb_ci_service');
        service.addQuery('sys_id', serviceSysId);
        service.query();
        if (service.next()) {
            gs.log('Thursday5' + incidentSysId, 'gov 03-11-2023');
            var serSysId = service.getValue('sys_id');
            serviceGroup = service.support_group;
        }
        var incAlert = new GlideRecord('incident');
        incAlert.addQuery('sys_id', incidentSysId);
        incAlert.query();
        if (incAlert.next()) {
            gs.log('Thursday6' + incidentSysId, 'gov 03-11-2023');
            var incidentType = incAlert.getDisplayValue('u_incident_type');
            var customField = incAlert.getDisplayValue('u_scom_customfield9');

            if ((customField == 'test') && (incidentType == 'test')) {
                incAlert.assignment_group = 'sys_id';
            } else if ((customField == 'test Team') && (incidentType == 'test')) {
                incAlert.assignment_group = 'sys_id';
            } else if ((customField == 'Ltest Team TEAM') && (incidentType == 'test')) {
                incAlert.assignment_group = 'sys_id';
            } else if ((customField == 'test') || (customField == 'testWinAdmin') && (incidentType == 'test')) {
                incAlert.assignment_group = 'sys_id';
            } else if ((customField == 'test') || (customField == 'test') && (incidentType == 'test')) {
                incAlert.assignment_group = 'sys_id';
            } else if ((customField == 'test') && (incidentType == 'test')) {
                incAlert.assignment_group = 'sys_id';
            } else if ((customField == 'test') && (incidentType == 'test')) {
                incAlert.assignment_group = 'sys_id';
            } else if ((customField == 'test Team') && (incidentType == 'ITCtestC')) {
                incAlert.assignment_group = 'sys_id';
            } else if ((customField == 'test') || (customField == 'Exchange') && (incidentType == 'test')) {
                incAlert.assignment_group = 'sys_id';
            } else {
                incAlert.assignment_group = serviceGroup;
               
            }
            gs.log('Thursday8' + incidentSysId, 'gov 03-11-2023');
            incAlert.cmdb_ci = ciSysId;
            incAlert.business_service = serSysId;
            incAlert.update();
        }
     },
    type: 'SBIncidentUtils'
 
Hi all,
In the above script include, script not going inside if loop if (alert.next()) {,
please help to find out the issue.
 
please help where i did mistake.
 
Thanks all
 

 

2 REPLIES 2

Aylee Andersen
Kilo Sage

Hi @MGR,

 

Sometimes, when you grab a field value using current.sys_id, it returns an object rather than a string of the sys_id. I would try casting it to a string by doing either current.sys_id + "" or current.getValue('sys_id'). Either of those should help. My guess is that alert.addEncodedQuery("incident="+incidentSysId) isn't making the query you expect it to. One way to test that theory is to add a gs.log('Encoded Query: ' + alert.getEncodedQuery()) after you query the em_alert table.

 

Hopefully that helps!

- Aylee

Hi @Aylee Andersen ,

Tested that after the alert.addEncodedQuery("incident="+incidentSysId) kept the log and its returning the sys_id in log but inside if condition its not going.