Transform Script.. i need to update user name based on it's country and currency.. how should i add multiple conditions?

ServiceNow SA
Kilo Guru

During importing I need to update user name based on it's country and currency.. how should i add multiple conditions?

1 ACCEPTED SOLUTION

Thank you for the clarification. I wasn't aware the data was being imported in to another table. I'm going to assume you have fields on your sys_user table called u_country and u_currency. You can adjust the field names as necessary. I'm also going to assume the import set field name is 'u_name' - again, adjust the script below as needed.



Put something like this in the script field of your transform map.



var user = new GlideRecord('sys_user');


user.addQuery('name', source.u_name);


user.addQuery('u_currency', source.u_currency);


user.addQuery('u_country', source.u_country);


user.query();



if (user.next()) {
        var target.u_user = user.getValue('sys_id');


        // User was found with name, currency, and country


} else {


        // user was not found with name, currency, and country


}


View solution in original post

13 REPLIES 13

Thank you for the clarification. I wasn't aware the data was being imported in to another table. I'm going to assume you have fields on your sys_user table called u_country and u_currency. You can adjust the field names as necessary. I'm also going to assume the import set field name is 'u_name' - again, adjust the script below as needed.



Put something like this in the script field of your transform map.



var user = new GlideRecord('sys_user');


user.addQuery('name', source.u_name);


user.addQuery('u_currency', source.u_currency);


user.addQuery('u_country', source.u_country);


user.query();



if (user.next()) {
        var target.u_user = user.getValue('sys_id');


        // User was found with name, currency, and country


} else {


        // user was not found with name, currency, and country


}


Thanks for your prompt reply..



But i do not have values in source... I have used below to split:


var _user = "ABC_USER US";


var user = _user.split('(');


var userName = user[0];


var country = user[1].split(' ')[2];



So now i need to write code based on this..


If you are running a transform script, then your import set record is your source.



Given what you have above to split the _user field in to the various parts, where is the currency you spoke of?



The sample you gave earlier was



ABC_USER USA USD



Which means your code should look more like this:



var user = source.u_user.split(' '); // save the record/field input in to an array split on the spaces


var userName = user[0];


var country = user[1];


var currency = user[2];


Okay.. so in that case too, that same script will work,., it will first search the user, if found , then it will look for his currency and country?


The complete script would look more like this. It says "Do you have any records matching this user, this country, and this currency? If so, do the first part of the if statement, otherwise, do what's in the else..." It's more efficient to get the database to do the filtering rather than retrieving 100 records then check the other two conditions in the JavaScript after retrieval.



var user = source.u_user.split(' '); // save the record/field input in to an array split on the spaces


var userName = user[0];


var country = user[1];


var currency = user[2];


var user = new GlideRecord('sys_user');


user.addQuery('name', userName);


user.addQuery('u_currency', currency);


user.addQuery('u_country', country);


user.query();



if (user.next()) {
        var target.u_user = user.getValue('sys_id');


        // User was found with name, currency, and country


} else {


        // user was not found with name, currency, and country


}