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

@CarolMa6 

so with the script include approach where are you stuck?

what debugging did you do?

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

Vishal Jaswal
Giga Sage

Hello @CarolMa6 

Script Include line#18: should not have comma at the end as it is the last one.
Script Include line#21: stringify is misspelled.

I believe you are trying to achieve something like below:

VishalJaswal_0-1750101441432.png

VishalJaswal_1-1750101450652.pngVishalJaswal_2-1750101458828.png

 

VishalJaswal_3-1750101473686.png


To achieve like above, here is all that you need:

1. UI Policy to hide variables:

VishalJaswal_4-1750101518746.png

2. onChange Client scripts = 2 ; onChange: request_for_you and onChange: name

onChange: request_for_you

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    //Type appropriate comment here, and begin script below
    var requestor = g_form.getValue('request_for_you');
    var userID = g_user.userID;
    if (requestor === 'Yes') {
        g_form.clearValue('user_name');
        g_form.clearValue('title');
        g_form.setMandatory('name', false);
        g_form.setReadOnly('name', true);
        g_form.setDisplay('user_name', true);
        g_form.setDisplay('title', true);

        // To populate other fields like pNumber, department and manager of the logged in user
        var ga = new GlideAjax('SARBGetUserDetails');
        ga.addParam('sysparm_name', 'getUserDetails');
        ga.addParam('sysparm_user', userID);
        ga.getXMLAnswer(function(response) {
            var userDetails = JSON.parse(response);
            if (userDetails.error) {
                g_form.showFieldMsg('name', userDetails.error, 'error');
                return;
            }
            g_form.setValue('name', userID);
            g_form.setValue('user_name', userDetails.user_name);
            g_form.setValue('title', userDetails.title);
        });

    } else if (requestor === 'Select') {

        g_form.setDisplay('user_name', false);
        g_form.setDisplay('title', false);
        g_form.clearValue('name');
        g_form.clearValue('user_name');
        g_form.clearValue('title');
        g_form.setMandatory('name', true);
        g_form.setReadOnly('name', false);


    } else if (requestor === 'No') {
        g_form.clearValue('name');
        g_form.clearValue('user_name');
        g_form.clearValue('title');
        g_form.setMandatory('name', true);
        g_form.setReadOnly('name', false);
        g_form.setDisplay('user_name', false);
        g_form.setDisplay('title', false);
    }

}


onChange: name

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

    var usrVal = g_form.getValue('name'); // sys_id of selected user
    var userID = g_user.userID;
    // If name is cleared, clear related fields
    if (!usrVal) {
        g_form.clearValue('user_name');
        g_form.clearValue('title');
        return;
    }

    // If selected user is current user, skip AJAX call

    if (usrVal === userID) {
        return;
    }

    g_form.setMandatory('name', true);
    g_form.setReadOnly('name', false);
    var ga = new GlideAjax('SARBGetUserDetails');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_user', usrVal);
    ga.getXMLAnswer(function(response) {
        var userDetails = JSON.parse(response);
        if (userDetails.error) {
            g_form.showFieldMsg('name', userDetails.error, 'error');
            return;
        }

        var userName = userDetails.user_name || '';
        var userTitle = userDetails.title || '';
        g_form.setValue('user_name', userName);
        g_form.setValue('title', userTitle);
        g_form.setDisplay('user_name', true);
        g_form.setDisplay('title', true);
        if (!userName) {
            g_form.showFieldMsg('user_name', 'User Name is not available for the selected user.', 'info');
        }
        if (!userTitle) {
            g_form.showFieldMsg('title', 'Title is not available for the selected user.', 'info');
        }
    });

}


Script Include: SARBGetUserDetails

VishalJaswal_5-1750101679046.png

 



var SARBGetUserDetails = Class.create();
SARBGetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserDetails: function() {
        var srn = this.getParameter('sysparm_user');
        var ast = new GlideRecord('sys_user');
        ast.addQuery('sys_id', srn);
        ast.setLimit(1);
        ast.query();
        if (!ast.next()) {
            return JSON.stringify({
                error: 'P/C number does not exist, please provide the correct one.'
            });
        }
        var userDetails = {
            name: ast.getValue('name'),
            user_name: ast.getValue('user_name'),
			title: ast.title.getDisplayValue()
        };
        return JSON.stringify(userDetails);
    },
    type: 'SARBGetUserDetails'
});


NOTE#1:  As you are using name variable type as Yes/No then by default Yes is selected, hence I set the Default value as Select.
NOTE#2: Please update the script include and client script based upon the userDetails you want to fetch and display.

VishalJaswal_6-1750101777080.pngVishalJaswal_7-1750101792064.png

 




Hope that helps!