- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
07-29-2019 10:11 AM
Hi all ,
I have a requirement to create a reference field called 'OPID' referenced to sys_user table and a tex field called
'Name ' .
OPID field is auto populated when form loads ( did this by adding javascript:gs.getUserID(); to the default value of OPID field ) it works fine .
and wrote a onload client script to populate the name field based on whats in OPID field .
 function onLoad() {
 //Type appropriate comment here, and begin script below
 
 var reqRec = g_form.getReference('u_opid');
 var grUser = new GlideRecord('sys_user');
 
 grUser.get(reqRec.sys_id);
 
 g_form.setValue('u_name', grUser.name);
 
}
I wrote the same script for different catalog items and they all working fine but now the same script is throwing below error . How do i fix this ? Please suggest .
Solved! Go to Solution.
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
07-29-2019 01:18 PM
Correct. I would change your current onLoad script to an onChange script and use the default for the initial data. The onChange could look like this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  //skip when loading, the default value will do the trick instead
  if (isLoading) {
    return;
  }
  //clear the name when the reference field is cleared
  if (newValue === "") {
    g_form.setValue("u_name", "");
    return;
  }
  var user = g_form.getReference("u_opid", showCallerName);
  function showCallerName(user) {
    g_form.setValue("u_name", user.name);
  }
}
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
07-29-2019 10:16 AM
try now
function onLoad() {
//Type appropriate comment here, and begin script below
var reqRec = g_form.getReference('u_opid', showcallerTitle);
function showcallerTitle(reqRec) {
g_form.setValue('u_name', reqRec.name);
}
}
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
07-29-2019 10:18 AM
You script should be something like
function onLoad() {
//Type appropriate comment here, and begin script below
 g_form.getReference('u_opid', doAlert); // doAlert is our callback function
}
 
function doAlert(caller) { // reference is passed into callback as first arguments
   g_form.setValue('u_name', caller.name);
}
					
				
			
			
				Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
07-29-2019 10:31 AM
One thing to note is that you should not use a GlideRecord query in client script as it is not best practice. That being said, you can use the examples provided by others or shorten it a little with the use of anonymous function:
function onLoad() {
//Type appropriate comment here, and begin script below
var reqRec = g_form.getReference('u_opid', function(reqRec){
  g_form.setValue('u_name', reqRec.name);
});
Regards,
Phuong
If you find my suggestions helpful, please mark it as correct or helpful to help out others as well 🙂
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
07-29-2019 12:22 PM
Instead of a client script, you could set the default value for that field as well:
javascript:gs.getUserDisplayName();
					
				
			
			
				
			
			
				
			
			
			
			
			
			
		