Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Location update upon CI tag

Abishek1998
Tera Contributor

Hi All,

 

So I have a requirement. Based on the CI name the location gets updated. Below is the script provided. The business rule is a before Insert/update business rule on alm_hardware table.

 

When ever the CI ( cmdb_ci_pc_hardware ) gets created with the hardware record, the CI name gets populated in the hardware table. I didn't do this requirement. while this action itself, the location should get updated based on CI name.

 

This is the Log I get when debugging. 

 

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

gs.info('Business Rule Triggered');

 

    var locationID = "760059cedb346b00aa21b4b8f4961942"; // ARN / SKYCITY

    var locationID1 = "30205182db746b00aa21b4b8f496193a"; // NO / OSL / TECH BASE

    var locationID2 = "8e6fa3c247f716103257c331336d434b"; // DK / CPH

 

if (!current.ci) {

gs.info('No CI associated, skipping location assignment...');

        return;

    }

 

    var ciName = '';

 

    // Fetch CI Name using GlideRecord

    var ciGR = new GlideRecord('cmdb_ci');

if (ciGR.get(current.ci)) {

ciName = ciGR.name; // Ensure 'name' field exists in cmdb_ci

    }

 

    if (!ciName) {

gs.info('CI Name is still empty, skipping location assignment...');

        return;

    }

 

    ciName = ciName.toLowerCase();

gs.info('CI Name Retrieved: ' + ciName);

 

    if (ciName.startsWith("arn")) {

        current.setValue('location', locationID);

    } else if (ciName.match(/^(no|osl|trd|svg|boo|tos|bgo)/)) {

        current.setValue('location', locationID1);

    } else if (ciName.match(/^(dk|cph|aal|aar|bll)/)) {

        current.setValue('location', locationID2);

    }

 

    current.update(); // Save the location change

 

})(current, previous);

1 ACCEPTED SOLUTION

Rohit  Singh
Mega Sage

Hi @Abishek1998 

 

What's the issue you are facing? Try to debug your code using gs.info()

 

In before BR don't need to have current.update() , it will work without that also.

 

If my response helped, please mark it helpful and accept the solution so that it benefits future readers.

 

Regards,

Rohit

View solution in original post

5 REPLIES 5

Rohit  Singh
Mega Sage

Hi @Abishek1998 

 

What's the issue you are facing? Try to debug your code using gs.info()

 

In before BR don't need to have current.update() , it will work without that also.

 

If my response helped, please mark it helpful and accept the solution so that it benefits future readers.

 

Regards,

Rohit