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 query multiple tables via one GlideRecord

amitroy
Tera Contributor

Hi All,

I have a requirement where a custom field 'Row/Rack location' which extends from the 'cmdb_ci' table needs to be updated on 26 different classes. Currently a Transform map is configured on one target table - cmdb_ci_hardware where it updates the row/rack location field to only this target table.

 

The requirement is to update around 26 different classes. There is no such mapping exists on those 26 tables.

Is there a way to include more classes on 1 transform map? Can we built a scrip (OnBefore) to customize the mapping?

 

 

 

1 ACCEPTED SOLUTION

Joshwa Antony S
Mega Guru

Yes you can. Try it out.

Create a property and store values (tables/classes)

example: incident,problem,change,sc_req_item // mention your classes here

var table = gs.getProperty("YOURPROPERTYNAME HERE").split(","); //this convert the comma separate string to array 

for(var i=0; i<table.length; i++){ // this will iterate based on number of table

var gr = new GlideRecord(table[i]);
gr.newRecord();
gr.short_description = 'TEST';
gr.insert();

}

Regards,

JAS

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Amit,

1 transform map can have only 1 target table.

Possibly you can have this running in onAfter transform script to update it for all other classes.

Regards
Ankur

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

amitroy
Tera Contributor

Thanks Ankur for your response. Is there an example you can help me with? What can be the parameters?

Below is the OnBefore script-

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

// Add your code here
var sname = source.u_device_name;
var gr = new GlideRecord('cmdb_ci_hardware');

gr.addQuery('name',sname);
gr.addEncodedQuery('sys_class_name!=sn_vul_qualys_ci^sys_class_name!=cmdb_ci_server');
//gr.addEncodedQuery('sys_class_name=u_voice^ORsys_class_name=u_ha_pair^ORsys_class_name=u_appliance');
gr.query();
if(gr.next()){
gr.u_row_rack_location = source.u_rack_name;

gr.update();
}

ignore = true;

})(source, map, log, target);

Hi Amit,

please try this in onafter for other classes

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

// Add your code here
var sname = source.u_device_name;
var gr = new GlideRecord('cmdb_ci_hardware');

gr.addQuery('name',sname);
gr.addEncodedQuery('sys_class_name!=sn_vul_qualys_ci^sys_class_name!=cmdb_ci_server');
//gr.addEncodedQuery('sys_class_name=u_voice^ORsys_class_name=u_ha_pair^ORsys_class_name=u_appliance');
gr.query();
if(gr.next()){
gr.u_row_rack_location = source.u_rack_name;

gr.update();
}

})(source, map, log, target);

Regards
Ankur

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

Joshwa Antony S
Mega Guru

Hi Amit,

If you need to create multiple records using single GlideRecrod, then use the below script

var table = ["incident","problem"]; // mention your classes here

for(var i=0; i<table.length; i++){ // this will iterate based on number of table

var gr = new GlideRecord(table[i]);
gr.newRecord
gr.short_description = 'TEST';
gr.insert();

}

I hope you can have some sort of script in your transform script.

Regards,

JAS