Business rule not updating the relationship type

Sumit Raj
Kilo Explorer

Hi Experts,

I am writing one after business rule on cmdb_ci_service_discovered table to update relationship type in cmdb_rel_ci table.

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

    // Add your code here
    var rel = new GlideRecord('cmdb_rel_ci');
    {
        rel.addQuery('child', current.sys_id);
        rel.addQuery('type', '55c95bf6c0a8010e0118ec7056ebc54d');
        rel.query();
        if(rel.next())
            {
                gs.log("test" + rel.getDisplayValue('type'));
                rel.type = '41008aa6ef32010098d5925495c0fb94';
                rel.update();
                gs.log("demo" + rel.getDisplayValue('type'));
            }
    }

})(current, previous);

But its not updating the record in the cmdb_rel_ci table. Is it like I would have to add new record and delete the existing one to achieve this?

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

try this and share the updates

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

    // Add your code here
    var rel = new GlideRecord('cmdb_rel_ci');
    rel.addQuery('child', current.sys_id);
    re.addQuery('type', '55c95bf6c0a8010e0118ec7056ebc54d');
    rel.query();
    if(rel.next())
    {
        gs.info("Record found");
        rel.type = '41008aa6ef32010098d5925495c0fb94';
        rel.update();
    }

})(current, previous);

Regards
Ankur

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

Hi Ankur,

I corrected the code as per your and Tony recommendation still its not updating the relationship. Also, I am not getting anything in the system log. I am using UI action "Convert to Application Service" to convert Business service. It is inserting one record in mapped application service table but the relationship is still "Contains::Contained by". It should change it to "Consumes::Consumed by" as per the business rule. Could please help me in this regard. Let me know if you need further input.

Try to debug by adding gs.info() statements.

Regards
Ankur

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

Tony Chatfield1
Kilo Patron

Hi, you have an error on this line

re.addQuery('type', '55c95bf6c0a8010e0118ec7056ebc54d');

missing the l from rel, try

rel.addQuery('type', '55c95bf6c0a8010e0118ec7056ebc54d');

Also there are a couple of {} that are not needed, and I recommend that you use the syntax of the example provided by Ankur Bawiskar