- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2014 07:32 AM
Hello All!
I have a new question to propose which extends off of a similar question I posted a week or so ago.
UI Policy script to set CI based on location
I would like to see how I could take the country variable that is returned and then go and get the sys_id of the "Support" Business Service for that location. For instance, if "US" is returned from the country variable, then the script would go and lookup Business Services named "Support" and return the sys_id of the one that has a location that contains US. That way, I can remove the manual hardcoding of the sys_id's and avoid having to revisit this script when another location is added.
function onCondition() { var userID = g_user.userID; var gr = new GlideRecord('sys_user'); var country = ''; if (gr.get(userID)) { country = gr.country; } //Lookup of "Support" CI based on the variable returned in country //Then maybe something like this: g_form.setValue('cmdb_ci', variable_of_returned_bs_sys_id); //Instead of this: if (country == "US") { g_form.setValue('cmdb_ci', '9nm6n7n8srmhkqgs5rybrvpzr9pbe889'); //sys_id for 'Support' CI in the USA } if (country == "DE") { g_form.setValue('cmdb_ci', 'rmevjm5re9e8srta9htv46w5geycxxfq'); //sys_id for 'Support' CI in Germany } }
Thanks in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2014 10:46 AM
Regarding gs, I had done that as part of my troubleshooting to make sure it wasn't an issue that I 'reused' gr a second time in the script. That turned out to not be an issue, but I hadn't changed it back.
I modified the script I had to take into account your revision, but then I realized that the location part of the addQuery was not working because it was looking to search by the country's sys_id and not the name. So, I put in another query to return the sys_id of the country that was found of the logged in user so that the Business Service query would work properly.
Here is what I have as a working script... but I am very open to comments if there is a better (or cleaner) way to use the script!
function onCondition() {
var userID = g_user.userID;
var gr = new GlideRecord('sys_user');
var country = '';
if (gr.get(userID)) {
country = gr.country;
}
var gr = new GlideRecord('cmn_location');
var countrysid = '';
gr.addQuery('name', country);
gr.query();
if(gr.next()){
countrysid = gr.sys_id;
}
var gr = new GlideRecord('cmdb_ci_service');
var sys = '';
gr.addQuery('name', 'Support');
gr.addQuery('location', countrysid);
gr.query();
if(gr.next()){
var sys = gr.sys_id;
}
g_form.setValue('cmdb_ci', sys);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2014 10:46 AM
Regarding gs, I had done that as part of my troubleshooting to make sure it wasn't an issue that I 'reused' gr a second time in the script. That turned out to not be an issue, but I hadn't changed it back.
I modified the script I had to take into account your revision, but then I realized that the location part of the addQuery was not working because it was looking to search by the country's sys_id and not the name. So, I put in another query to return the sys_id of the country that was found of the logged in user so that the Business Service query would work properly.
Here is what I have as a working script... but I am very open to comments if there is a better (or cleaner) way to use the script!
function onCondition() {
var userID = g_user.userID;
var gr = new GlideRecord('sys_user');
var country = '';
if (gr.get(userID)) {
country = gr.country;
}
var gr = new GlideRecord('cmn_location');
var countrysid = '';
gr.addQuery('name', country);
gr.query();
if(gr.next()){
countrysid = gr.sys_id;
}
var gr = new GlideRecord('cmdb_ci_service');
var sys = '';
gr.addQuery('name', 'Support');
gr.addQuery('location', countrysid);
gr.query();
if(gr.next()){
var sys = gr.sys_id;
}
g_form.setValue('cmdb_ci', sys);