Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Auto Populate Current User First and Last name ( String ) in scoped App

Raj23
Tera Contributor

I created an Employee Entry form in a scoped app, Created First and Last name ( String ) field, would like to auto populate the first and last name of the current user.  Can some one help me with the script for the scoped app

 

 

Raj23_1-1720180475415.png

 

4 REPLIES 4

Community Alums
Not applicable

Hi @Raj23 ,

You can write a onLoad Client script using g_form API, I've provided the code below:

Feel free to customize the code according to you:

(function executeRule(current, previous /*null when async*/) {
    // Ensure we are in a scoped application
    if (typeof g_user !== 'undefined') {
        // Retrieve the first name and last name of the current user
        var firstName = g_user.firstName;
        var lastName = g_user.lastName;

        // Set the field values
        g_form.setValue('first_name', firstName);
        g_form.setValue('last_name', lastName);
    }
})(current, previous);

 

Thanks.

Hope it helps.

If my response turns useful mark it helpful and accept solution😊.

Kieran Anson
Kilo Patron

Another option is to do this within the dictionar entries' default value field.

Screenshot 2024-07-05 at 14.11.10.png

javascript: gs.getUser(). getFirstName()
javascript: gs.getUser(). getLastName()

I tried the same for Department field in the form but didn't had the same behavior. 

 

Raj23_0-1720421263889.png

 

The syntax in your screenshot doesn't look correct, it should be colon ":" after the word javascript, not a semicolon ";"