Populate Logged in user details in catalog item

Somasekhar6
Tera Contributor

HI, Developers I have below the Type of variables in my catalog item. How can I get These Details with the Script?

 

Thanks for your time and support.

 

Requestor NameCapture the Full Name of the logged in user
Requestor LocationCapture the "Location Name" of Requested For from sys_user table
Requestor Mobile NumberCapture the "Mobile Phone" of Requested For from sys_user table
Project Name (ID)Look-up to project IDs associated with Requested For

 

6 REPLIES 6

Anil Lande
Kilo Patron

Looks like you have posted the complete requirement here.

Can you please share what you have tried and where exactly you are stuck?

 

 

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

HI, @Anil Lande  I am very new to ServiceNow One of my friends works in ServiceNow I just collected the use case for my knowledge improvement as I am very new to ServiceNow. 

Mehta
Kilo Sage
Kilo Sage

@Somasekhar6 

Inorder to achieve this based on requested for variable. you need to write a onchange client script and then call the client callable script include through glide ajax and populate the variables. 

 

Below is the sample client script code : 

    var user = g_form.getValue('requested_for'); // requested for
    var ajax = new GlideAjax('OffBoarding_Autopopulate'); // script include name
    ajax.addParam('sysparm_name', 'getUserDetails'); // function name
    ajax.addParam('sysparm_user', user); // user
    ajax.getXML(userParse); // call back function

    function userParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var users = JSON.parse(answer); // parse response
        g_form.setValue('location', users[0].location); //set department
        g_form.setValue('phone', users[0].phone); //set phone
        g_form.setValue('email', users[0].email); //set email
		g_form.setValue('title',users[0].title);// set job title
		g_form.setValue('manager', users[0].manager); //set manager
    }

 

Below is the client callable script include sample code: 

 

 getUserDetails: function() {
        var details = [];
        var user = this.getParameter('sysparm_user');
        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('sys_id', user);
        userGR.query();
        while (userGR.next()) {
            var users = {};
            users.location = userGR.loaction.toString();
            users.email = userGR.email.toString();
            users.title = userGR.title.toString();
            users.phone = userGR.phone.toString();
            users.managerEmail = userGR.manager.email.toString();
            users.manager = userGR.manager.name.toString();
            details.push(users);
        }
        return JSON.stringify(details);
    },

 

Please mark it as correct answer, if resolves your query.

hi @Mehta  thanks for your solution but in my case, the requester name variable is a read-only variable in the service catalog it has to be filled with the currently logged-in user name based on his name only I need to populated the rest of the data.