The CreatorCon Call for Content is officially open! Get started here.

getreference

Ashley
Kilo Sage

Hello,

First attempt at scripting:

I am trying to write a client script within a Variable Set that returns the current logged in user Floor, managed to get Location, Department etc working as these have pre-built methods:

Floor is a custom field that has been added so wont work,

Here is the script I have so far which is currently not working:

______________________________________________________________

function onChange(control, oldValue, newValue, isLoading) {

  var userLocID = g_form.getReference('u_locationid', Display);

  }

function Display(userLocID) {

  if (userLocID != ' ')

  g_form.setValue('Floor', userLocID);

  }

Comments:

u_locationid is the Column Name on the User Table where the Floor is added as part of the import

Floor is the name of the field on the New User Request Form

Any help would be appreciated

Ashley

1 ACCEPTED SOLUTION

Hi Tanaji,



Thank you for your reply



I should have updated this ticket a bit sooner,



I worked with one of the developers in the office to get this working and here is the code we used:



function onLoad() {


var userLocID = g_user.userID;


var FloorQ = new GlideRecord('sys_user');


FloorQ.addQuery('sys_id', userLocID);


FloorQ.query();


if (FloorQ.next()) {        


g_form.setValue('u_floor', FloorQ.u_locationid);   }


}


View solution in original post

7 REPLIES 7

Mike Allen
Mega Sage

I would think that 'Floor' is actually named 'floor' or 'u_floor'.   You are trying to get it from the user record?   So, 'u_locationid' is not on this form.   var userLocID = g_form.getReference('u_locationid', Display); needs to be the reference field on the form, so like var userLocID = g_form.getReference('caller_id', Display); or whatever.



Your function, Display, should be named display to come into javascript best practice, though it is not required.



You function probably should say something along the lines of:



function Display(userLocID) {


  if (userLocID != ' ')


  g_form.setValue('Floor', userLocID.u_floor);


  }


Hi Mike,



u_locationid (String field) is on the User Record


Floor is a field I created on a form which I want to populate from the User record based on the person logged in (u_locationid)



Hope that makes sense



I will have a play with the code.


Hi Ashley,



Form what I understand, you want to set the 'Floor' variable to current user's(i.e. logged in user's) u_locationid(this field is on user record).



if you want Floor variable to get populated as soon as the your new user request form is loaded then you need an onLoad catalog client script with below code-


function onLoad() {


var currentUserSysID = g_user.userID;   //get the sys_id of current user


var userRecord= new GlideRecord('sys_user');


userRecord.get(currentUserSysID);         //get the current user record


g_form.setValue('Floor',userRecord.u_locationid);         //set Floor with the value in the LocationID field on the current user record


}



There are ways to reduce this server call but I would like to keep the code simple and straight forward for you keeping in mind this is your attempt.



If you want to set the Floor variable to get populated depending on selection/value of any other field then you can use the same code in the onChange client script.


Also if you have any reference field referencing the current user record then you can also use getReference() instead of doing GlideRecord().



Thanks,


Tanaji Patil



--Mark correct/helpful if it helps solving your issue.


Hi Tanaji,



Thank you for your reply



I should have updated this ticket a bit sooner,



I worked with one of the developers in the office to get this working and here is the code we used:



function onLoad() {


var userLocID = g_user.userID;


var FloorQ = new GlideRecord('sys_user');


FloorQ.addQuery('sys_id', userLocID);


FloorQ.query();


if (FloorQ.next()) {        


g_form.setValue('u_floor', FloorQ.u_locationid);   }


}