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.

Whats wrong with below script?

dvelloriy
Kilo Sage

Hello All,

I am trying to set few fields on incident form using a transform map however i dont see values coming in.

What is wrong in below script? can someone advise here?

For CI mapping, please note that class should contain server because in cmdb we have different ci's with hostnames extracted in different classes like virtual instances and all. 

-------------------------

 

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

var str = source.tags;
var ci = '';
var suppG = '';
var arr = str.split(',');
for (i = 0; i < arr.length; i++) {
    if (arr[i].indexOf('Hostname:') == 0) {
        ci = arr[i].split(':')[1];
    }
    if (arr[i].indexOf('Product Team') == 0) {
        suppG = arr[i].split(':')[1];
    }
}


var ciGr = new GlideRecord('cmdb_ci');
    ciGr.addQuery('name', ci);
    ciGr.addQuery('class','CONTAINS','server');
    ciGr.query();
    if (!ciGr.next())
     ci = 'CI Not Found - Hardware';
   

var grpGR = new GlideRecord('sys_user_group');
    grpGR.addQuery('name', suppG);
    grpGR.query();
    if (!grpGR.next()){

    var cmdbGr = new GlideRecord('cmdb_ci');
        cmdbGr.addQuery('name', ci);
        cmdbGr.query();
        if (cmdbGr.next())
        suppG = cmdbGr.support_group;
    }

target.cmdb_ci = ci;
target.assignment_group = suppG;
target.u_resolution_ci = ci;

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

rseth91
Tera Contributor

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

var str = source.tags.toString() || '';
var ci = '';
var suppG = '';
var arr = str.split(',');

for (var i = 0; i < arr.length; i++) {
if (arr[i].indexOf('Hostname:') === 0) {
ci = arr[i].split(':')[1].trim();
}
if (arr[i].indexOf('Product Team') === 0) {
suppG = arr[i].split(':')[1].trim();
}
}

var ciGr = new GlideRecord('cmdb_ci');
ciGr.addQuery('name', ci);
ciGr.addQuery('class', 'CONTAINS', 'server');
ciGr.query();

if (!ciGr.next()) {
ci = 'CI Not Found - Hardware';
}

var grpGR = new GlideRecord('sys_user_group');
grpGR.addQuery('name', suppG);
grpGR.query();

if (!grpGR.next()) {
var cmdbGr = new GlideRecord('cmdb_ci');
cmdbGr.addQuery('name', ci);
cmdbGr.query();
if (cmdbGr.next()) {
suppG = cmdbGr.support_group;
}
}

target.cmdb_ci = ci;
target.assignment_group = suppG;
target.u_resolution_ci = ci;

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

Can you try this

SumanKumarM
Tera Contributor

Hello Dvelloriy,

Would you please check if below str is returning value. I don't see field 'source.tags in cmdb_ci, instead I see 

'sys_tags' field. Please debug using log statements to analyse further.
 
Please mark helpful, if it helps you debug further.
 
BR,
Suman.

Anand Kumar P
Giga Patron

Hi @dvelloriy ,

Try changing below lines and see if it works for you 

 

ciGr.addEncodedQuery('sys_class_nameLIKEserver');

 

suppG = cmdbGr.support_group + '';

cmdb_ci is reference field so it will expect sys_id not string.

target.cmdb_ci = ciGr.sys_id;
target.u_resolution_ci = ciGr.sys_id;

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand