Update single line field content if user changed

Julius28
Tera Expert

Hi!

Currently i have catalog item, which has some fields and one of them is reading logged in user info like mobile phone number and adds it in one of single line text fields.

User field is ordering_for and field where phone number is displayed is called 'mobile_phone_number' and in it has Default Value with following: javascript:gs.getUser().getRecord().getValue('mobile_phone');

which basically reads mobile_phone from table.

The problem is now, if i change user in ordering_for, it still shows in phone number field logged in user number(since it has getUser) not the one in ordering_for.

Should I remove completely default value and write some on change script or i can add some additional function in Default value? And if I can update Default value, what should be there added?

 

6 REPLIES 6

Sujatha V M
Kilo Patron
Kilo Patron

@Julius28 You on create an onChange client script for "ordering_for" and call the script include as below to populate the value. 

 

Client script : 

var usr = new GlideAjax('UserDetails');
usr.addParam('sysparm_name','getUserFields');
usr.addParam('sysparm_user',g_form.getValue('<field name>'));
usr.getXML(getDetails);
 
function getDetails(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
var userVal = JSON.parse(answer);
g_form.setValue('phone' , userVal .phone);
}
}

 

Script include : 

var UserDetails = Class.create();
UserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
getUserFields: function () {
var usrID = this.getParameter('sysparm_user');
var obj = {};
var usrData = new GlideRecord('sys_user');
if(usrData.get(usrID)){
obj.phone = usrData.getValue('phone'); //Phone details 
}
var usrDetail = new JSON();
var userData = usrDetail.encode(obj);
return userData;
},
    type: 'UserDetails'
});
 
Note: Apart from Phone detail, you can cascade other details of the user record. 
 
Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

 
Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

James Chun
Kilo Patron

Hi @Julius28,

 

If you are not using the 'Requested for' type variable, you can set the default value of the 'ordering_for' variable as the current user. (e.g. javascript&colon; gs.getUserID();)

 

Then with the 'mobile_phone_number' variable, remove the default value and use the 'Auto-populate' functionality. Something like the below:

JamesChun_0-1712654179663.png

Cheers

Hi @James Chun ,

Forgot to mention,  I am using Requested for. Will it work?

 

Thanks.

Yup, if you are using 'Requested for' variable, it should auto-populate with the current user.

Hence you don't need the default value for the variable.

 

Then use the same logic as above to get the mobile number using the 'Auto-populate'.

 

Cheers