Populate conditions value of a reference field via Script

Community Alums
Not applicable

Hey!

I want to populate the conditions field in ServiceNow via script.

Scenario - We have requirement to populate the data in Special Handling notes table via excel sheet ,

For that I have created an import set , but I need to understand how we can populate the display value of reference field.

gauravtygai_0-1706164246120.png

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

So here is the answerrr,

Just map the fields and map the field which you want to add in conditions with excel.

After that write an OnAfter Transform Script - 

 

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

var gt = new GlideRecord('sn_shn_notes');
gt.addQuery('sys_id', target.sys_id);  //sys_id of created record
gt.query();
if (gt.next()) {
//gs.info('GOT THE MANAGER SYS_ID -- sys id of user'+target.sys_id);
var getUser = new GlideRecord('sys_user');
getUser.addQuery('name', source.u_primary_contact); //reference field value from excel
getUser.query();
if (getUser.next()) {
var sys_id = 'manager=' + getUser.sys_id;
gt.conditions = sys_id;
gt.update();
}
} else {
gs.info("GOT THE MANAGER SYS_ID -- record not found");
}

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

View solution in original post

3 REPLIES 3

Anil Lande
Kilo Patron

Hi,

You need to ask them to share user_name or email details (unique) field data to identify user record. Using name of user may update incorrect values in the target table.

Once you get unique value for user you can use transform script (field mapping level) and get the user sys_id for given value and generate the encoded query(condition).

To update condition field you need to set value like

"manager=<sys_id of manager>"

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Community Alums
Not applicable

Hi Anil ,

Thanks for the response .

I am trying to create a field map script for the field primary contract(Staging table field name) 

How should i write the "return" value -

manager = sys_idOfUser i fetched above. 

 

And also I want to abort the action if manager is not present.

gauravtygai_0-1706174201543.png

 

Community Alums
Not applicable

So here is the answerrr,

Just map the fields and map the field which you want to add in conditions with excel.

After that write an OnAfter Transform Script - 

 

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

var gt = new GlideRecord('sn_shn_notes');
gt.addQuery('sys_id', target.sys_id);  //sys_id of created record
gt.query();
if (gt.next()) {
//gs.info('GOT THE MANAGER SYS_ID -- sys id of user'+target.sys_id);
var getUser = new GlideRecord('sys_user');
getUser.addQuery('name', source.u_primary_contact); //reference field value from excel
getUser.query();
if (getUser.next()) {
var sys_id = 'manager=' + getUser.sys_id;
gt.conditions = sys_id;
gt.update();
}
} else {
gs.info("GOT THE MANAGER SYS_ID -- record not found");
}

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