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.

How to update different two tables values based on single catalog item input

rajibosss11
Tera Contributor

I need to update CMDB_ci record manufacturer name and the same time need to i need to update cmdb_ci_aix_server table record approval group name but both the record have similar ci name eg : in cmdb_ci table have "TEST" name with two records one is belongs to cmdb_ci and another record is related to cmdb_ci_aix_server , so here manufacturer need to chang for cmdb record and approval group need to change for server record via catalog item how to archive this .

 

could you check and below code and confirm this for Action.

 

 

(function execute(inputs, outputs) {
var ciName = inputs.ci_name;
var newManufacturer = inputs.new_manufacturer;
var newApprovalGroup = inputs.new_approval_group;

// Update manufacturer on base cmdb_ci
var baseCI = new GlideRecord('cmdb_ci');
baseCI.addQuery('name', ciName);
baseCI.addQuery('sys_class_name', 'cmdb_ci'); // Ensures it's only base
baseCI.query();
if (baseCI.next()) {
baseCI.manufacturer = newManufacturer;
baseCI.update();
}

// Update approval group on cmdb_ci_aix_server
var aixCI = new GlideRecord('cmdb_ci_aix_server');
aixCI.addQuery('name', ciName);
aixCI.query();
if (aixCI.next()) {
aixCI.u_approval_group = newApprovalGroup; // Use the actual field name
aixCI.update();
}

})(inputs, outputs);

1 ACCEPTED SOLUTION

tiagomacul
Giga Sage

sys_class_name filter might not behave as expected

try to debug

 

 

(function execute(inputs,  outputs)  {
       var ciName  =  inputs.ci_name;
       var newManufacturer  =  inputs.new_manufacturer;
       var newApprovalGroup  =  inputs.new_approval_group;

      //  Update  manufacturer  on base  cmdb_ci
       var  baseCI =  new  GlideRecord('cmdb_ci');
       baseCI.addQuery('name', ciName);
       baseCI.query();
       while  (baseCI.next()) {
              if  (baseCI.getTableName()  === 'cmdb_ci')  {
                     baseCI.manufacturer  = newManufacturer;
                     baseCI.update();
                     gs.info('Updated  manufacturer for  base  CI:  ' +  ciName);
              }
       }

       //  Update  approval group  on  cmdb_ci_aix_server
       var aixCI  =  new  GlideRecord('cmdb_ci_aix_server');
      aixCI.addQuery('name',  ciName);
       aixCI.query();
       while (aixCI.next())  {
              aixCI.u_approval_group  = newApprovalGroup;
              aixCI.update();
              gs.info('Updated  approval group  for  AIX  server CI:  '  +  ciName);
      }

})(inputs,  outputs);

View solution in original post

1 REPLY 1

tiagomacul
Giga Sage

sys_class_name filter might not behave as expected

try to debug

 

 

(function execute(inputs,  outputs)  {
       var ciName  =  inputs.ci_name;
       var newManufacturer  =  inputs.new_manufacturer;
       var newApprovalGroup  =  inputs.new_approval_group;

      //  Update  manufacturer  on base  cmdb_ci
       var  baseCI =  new  GlideRecord('cmdb_ci');
       baseCI.addQuery('name', ciName);
       baseCI.query();
       while  (baseCI.next()) {
              if  (baseCI.getTableName()  === 'cmdb_ci')  {
                     baseCI.manufacturer  = newManufacturer;
                     baseCI.update();
                     gs.info('Updated  manufacturer for  base  CI:  ' +  ciName);
              }
       }

       //  Update  approval group  on  cmdb_ci_aix_server
       var aixCI  =  new  GlideRecord('cmdb_ci_aix_server');
      aixCI.addQuery('name',  ciName);
       aixCI.query();
       while (aixCI.next())  {
              aixCI.u_approval_group  = newApprovalGroup;
              aixCI.update();
              gs.info('Updated  approval group  for  AIX  server CI:  '  +  ciName);
      }

})(inputs,  outputs);