Auto populate user details based on a question

CarolMa6
Tera Expert

Hi, 

Some help please not sure what am i missing here either of the user details are not populating at all. 

 

These are the variables i created: 

CarolMa6_0-1750090543858.png

 

script include i created: 

CarolMa6_1-1750090602058.png

Catalog client script

CarolMa6_2-1750090686699.png

CarolMa6_3-1750090700247.png

 

Regards 

CarolMa

1 ACCEPTED SOLUTION

@CarolMa6 

In this scenario, if user selects Yes, then Auto-populate the Requested for field with the currently logged in user's id and make the field read-only. You can achieve this using UI policy.

If user selects no, then leave it as is.

Regards,

Siva

View solution in original post

11 REPLIES 11

Hi @CarolMa6 ,

I have update my last response incase if you have missed it

 

update the client script as below

 

u are  running this on Change of request_for_you variable only right?

 function onChange(control, oldValue, newValue, isLoading) {
     if (isLoading || newValue == '') {
         return;
     }

     var requestor = g_form.getValue('request_for_you');

     if (request_for_you == 'Yes') {
         var ga = new GlideAjax('SARBGetUserDetails');
         ga.addParam('sysparm_name', 'getUserDetails');
         ga.addParam('sysparm_user', g_user.getUserID());
         ga.getXMLAnswer(function(response) {
             var UserDetails = JSON.parse(response);
             g_form.setValue('name', UserDetails.userName);
             g_form.setValue('p_number', UserDetails.pNumber);
             g_form.setValue('department', UserDetails.department);
             g_form.setValue('line_manager', UserDetails.managerName);
         });
     } else {
         g_form.setValue('name', "");
         g_form.setValue('p_number', "");
         g_form.setValue('department', "");
         g_form.setValue('line_manager', "");
     }
 }

 

if the line_manager and department are of string type

use

var SARBGetUserDetails = Class.create();
SARBGetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserDetails: function() {
        var UserDetails = {};
        var ast = new GlideRecord("sys_user");
        if (ast.get(this.getParameter("sysparm_user"))) {
            UserDetails = {
                "userName": ast.getValue('name'),
                "pNumber": ast.getValue('user_name'),
                "department": ast.department.getDisplayValue(),
                "managerName": ast.manager.getDisplayValue(),
            };
        }
        return JSON.stringify(UserDetails);
    },
    type: 'SARBGetUserDetails'
});

if they are of reference type use

 var SARBGetUserDetails = Class.create();
 SARBGetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

     getUserDetails: function() {
         var UserDetails = {};
         var ast = new GlideRecord("sys_user");
         if (ast.get(this.getParameter("sysparm_user"))) {
             UserDetails = {
                 "userName": ast.getValue('name'),
                 "pNumber": ast.getValue('user_name'),
                 "department": ast.getValue('department'),
                 "managerName": ast.getValue('manager'),
             };
         }
         return JSON.stringify(UserDetails);
     },
     type: 'SARBGetUserDetails'
 });

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

J Siva
Tera Sage

Hi @CarolMa6 

Use "Auto-populate" feature to populate the related values. No need to use scripts and easy to maintain.

Screenshot_20250616-220112.png

Regards,

Siva

Ankur Bawiskar
Tera Patron
Tera Patron

@CarolMa6 

you can use Auto populate feature and no scripting is required

Auto-populate a variable based on a reference type variable (Utah) 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

CarolMa6
Tera Expert

@Ankur Bawiskar @J Siva 

 

Not sure how will this solution work because auto populating the fields is dependent on the "request_for_you" variable which is a "yes/no" answer. If answer is yes, the form should auto populate the logged in user details if answer is no, then user needs to populate the requested for name then the username, department and line manager should auto populate. 

 

CarolMa6_0-1750092631928.png

 

@CarolMa6 

In this scenario, if user selects Yes, then Auto-populate the Requested for field with the currently logged in user's id and make the field read-only. You can achieve this using UI policy.

If user selects no, then leave it as is.

Regards,

Siva