- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2017 03:15 PM
I have a field on a record producer called "Affected User", that when the user name is entered I want a 2nd field to populate automatically with data from a field on that user's record. In this case specifically a field for "Asset", which is the user's computer. Secondly, if the user has more than 1 asset listed in their user record (i.e. they have more than 1 computer assigned to them), I want the 2nd field to populate with all the assets for that user so that the user can select an asset to designate for the record producer. The record producer is for identifying issues with a computer for example. thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2017 05:01 PM
ok this is what I think that you want to accomplish
If the field user name is filled then the field asset show the hardware assets assigned to that user,
To do that the field user name must be a reference to the table 'sys_user'
and the field Asset must be a reference to the table 'alm_hardware' and you can use the script for advance Ref Qual in this field like this
var user = current.variables.cur_user;
return 'assigned_to=' + user;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2017 10:04 PM
Ok this is what you need to do
1.- Check that your fields be like this:
> User is Reference to [sys_user]
>Current Computer is Reference to [alm_hardware]
2.- Set the advance RefQual to Current Computer= javascript: new user_asset().assets();
Create a script include named user_asset with this function:
assets: function(){
var user = current.cur_user.sys_id;
return 'assigned_to='+user+'^model_category=81feb9c137101000deeabfc8bcbe5dc4'; //Returns only the model category 'computers' check the sys_id in case that it be different
},
3.-Create a Client Script in the table, this is the Best Practices to autopopulate a field. not the RefQualifier:
>the Client Script is 'On Change' of 'Current User'
>The code is=
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var count = 0;
var gr = new GlideRecord('alm_hardware');
gr.addQuery('assigned_to',newValue);
gr.addQuery('model_category','81feb9c137101000deeabfc8bcbe5dc4');//Computers
gr.query();
while(gr.next()){
count++;
}
if(count==1){
g_form.setValue('u_hardware',gr.sys_id);
}else{
g_form.setValue('u_hardware','');
}
//Autopopulates if is only one computer Assigned or Clear the field if there is more than one in case that the user changes
}
If this is what you need please mark as correct or helpfull, thanks!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2017 09:41 AM
right now we already have the reference for Current Computer set to cmdb_ci_computer and the advanced reference qualifier set to:
javascript: 'assigned_to="+current.variables.cur_user;
is there a way to achieve what we are shooting for while keeping this as is?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2017 10:28 AM
yes you only have to target to cmdb_ci_computer instead of alm_hardware because both have the field 'assigned_to' and refers to the same asset
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2017 03:40 PM
gave it a shot and it's not working...maybe I need to make more changes due to the reference and the advanced reference qualifier I'm needing to keep as listed above.
I created the new catalog client script, onChange , with Variable name cur_user (field which pulls from sys_user)
the script I have on the catalog client script is:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var count = 0;
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('assigned_to',newValue);
gr.addQuery('model_category','81feb9c137101000deeabfc8bcbe5dc4');//Computers
gr.query();
while(gr.next()){
count++;
}
if(count==1){
g_form.setValue('u_computer',gr.sys_id);
}else{
g_form.setValue('u_computer','');
}
//Autopopulates if is only one computer Assigned or Clear the field if there is more than one in case that the user changes
}
I then created the script include named "user_asset" with this code:
var user_asset = Class.create();
user_asset.prototype = {
assets: function(){
var user = current.cur_user.sys_id;
return 'assigned_to='+user+'^model_category=81feb9c137101000deeabfc8bcbe5dc4'; //Returns only the model category 'computers' check the sys_id in case that it be different
},
type: 'user_asset'
};
see an issue with my codes?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2017 04:11 PM
you don ´t need the model category in the client script and the script include because is the cmdb_ci_computer gives only computers.
and remember to use the reference qualifier in the Current Computer field like this: javascript: new user_asset().assets();