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

Auto populate depertment field

asd22
Tera Contributor

Hello. 

 

I have a script that works fine with auto populating fields when a user is selected:

asd22_0-1705490057375.png


My issue is it gets ID and status fine and displays it, but when i tried with setvalue on Department it only showed the sysID. I tried with getdisplayvalue, but now it does not display anything.

1 ACCEPTED SOLUTION

@asd22 

 

dot walk shoudl be sys_user.departmnet

May i know what is reason of u_department

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

View solution in original post

14 REPLIES 14

Okay @asd22 , but this is OOTB so I suggested. 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Ehab Pilloor
Mega Sage

Hi @asd22,

The g_form.getDisplayValue() function is particularly useful when you want to capture the value of a field as it appears to the end user, which may differ from the actual underlying value stored in the database. This is because it takes into account field formatting, choice lists, and any translations applied. 

Try setting the values outside of the function. Remove the function from getReference.

 

var user_ref = g_form.getReference('var1_name'); 
alert(user_ref); 
g_form.setValue('ID', user_ref.user_name); 
g_form.setValue('unit', user_ref.department);
g_form.setValue('status', user_ref.status);

 

If you found this reply useful, please mark it as solution/helpful.

 

Thanks and Regards,

Ehab Pilloor

Service_RNow
Mega Sage

Hi @asd22 

Plz try to Glide Ajax to call a script include then return the user values as needed.

 

Create a script inclde get the data makesure Client callable is true.
var loaner = Class.create();
loaner.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    getdpt: function() {
        var obj = {};
        var sysid = this.getParameter('sysparm_usrsysid');
        var grusr = new GlideRecord("sys_user");
        grusr.addQuery('sys_id', sysid);
        grusr.query();
        if (grusr.next()) {
			
            obj.department = grusr.getValue('department');
        }
        return JSON.stringify(obj);
    },


    type: 'loaner'
});
On change client script:-
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var name = g_form.getValue('requested_for');
    var user = new GlideAjax('global.loaner');
    user.addParam('sysparm_name', 'getdpt');
    user.addParam('sysparm_usrsysid', name);
    user.getXML(getData);
}

function getData(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    var data = JSON.parse(answer);
    g_form.setValue('department', data.department);

}

 

 Please mark reply as Helpful/Correct, if applicable. Thanks!

Amit Verma
Kilo Patron
Kilo Patron

Hi @asd22 

 

If you want to do this using code based approach, please refer the following blog https://www.servicenow.com/community/developer-blog/get-display-value-of-reference-variable-service-...

 

Although, a no code approach as suggested by @Dr Atul G- LNG  will be a better alternative.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

asd22
Tera Contributor

the no code im not sure how it should be set up. Its not the login user that autopopulates the info in the singeltextfield. Its when i select a user from a "reference" field it auto populates info in the textboxes