Business Rule isn't working

0890KM
Tera Contributor

Hey All,

 

I'm having issues with my business rule. I'm doing service mapping requirement in which a parent ci needs the correct child cis to be mapped to it. 

 

Currently, I have a business rule that in onBefore that calls my script include and my function. I need it to prevent duplicates for adding on the table. 

 

business rule 

onBefore() on insert and update table cmdb_rel_ci

code for script include 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    new getIPAddressesForCMDB.relationshipExists(current.parent, current.child);
    gs.addErrorMessage("This is a duplicate found." + current.parent.getDisplayValue() + " " + current.child.getDisplayValue());
    current.setAbortAction(true);

})(current, previous);

 

My script include with the function...only posting the functions that are important. 

 function relationshipExists(parentSysID, childSysID) {
            var relGR = new GlideRecord('cmdb_rel_ci');
            relGR.addQuery('parent', parentSysID);
            relGR.addQuery('child', childSysID);
            relGR.addQuery('type', getConsumesRelType());
            relGR.query();
            return relGR.hasNext();


        }

        function getConsumesRelType() {
            var typeGR = new GlideRecord('cmdb_rel_type');
            typeGR.addQuery('parent_descriptor', 'Contains');
            typeGR.addQuery('child_descriptor', 'Contained by');
            typeGR.query();
            if (typeGR.next()) {
                return typeGR.sys_id;
            } else {
                gs.warn('Contains::Contained by relationship type not found');
                return null;
            }
        }



    },

    type: 'getIPAddressesForCMDB'
};

 

 

 

 

What am I doing wrong? When I try to insert a record on that cmdb_rel_ci it still inserts. By business rule should prevent this. 

 

1 ACCEPTED SOLUTION

After updating the script include as shared 

 

Yes, also as shared by @palanikumar 

Update BR script by adding parenthesis 

new getIPAddressesForCMDB().relationshipExists(current.parent, current.child);

 

 

Regards 

Chaitanya 

View solution in original post

3 REPLIES 3

Chaitanya ILCR
Kilo Patron

var getIPAddressesForCMDB = Class.create();

getIPAddressesForCMDB.prototype = {

    initialize: function() {

  

    },

 

    relationshipExists: function(parentSysID, childSysID) {

        var relGR = new GlideRecord('cmdb_rel_ci');

        relGR.addQuery('parent', parentSysID);

        relGR.addQuery('child', childSysID);

        relGR.addQuery('type', this.getConsumesRelType());

        relGR.query();

        return relGR.hasNext();

    },

    getConsumesRelType: function() {

        var typeGR = new GlideRecord('cmdb_rel_type');

        typeGR.addQuery('parent_descriptor', 'Contains');

        typeGR.addQuery('child_descriptor', 'Contained by');

        typeGR.query();

        if (typeGR.next()) {

            return typeGR.sys_id;

        } else {

            gs.warn('getIPAddressesForCMDB: Contains::Contained by relationship type not found');

       

    return null;

        }

    }

};

 

 

 

Hi @0890KM 

Convert your script include into this

 

 

 

 

 

After updating the script include as shared 

 

Yes, also as shared by @palanikumar 

Update BR script by adding parenthesis 

new getIPAddressesForCMDB().relationshipExists(current.parent, current.child);

 

 

Regards 

Chaitanya 

palanikumar
Mega Sage

There is a problem in your Business rule. Parentheses(bracets) missing for Class initialization 

Use this updated Line:

new getIPAddressesForCMDB().relationshipExists(current.parent, current.child);

 

 

Thank you,
Palani