How to Auto Populate email id of user in email field whose name is specified in Requested For field of a Request form.

RAJJYOTI
Tera Contributor

How to Auto Populate email id of user in email field whose name is specified in Requested For field of a Request form.

4 REPLIES 4

BALAJI40
Mega Sage

Hi,

Create an onChange script on requested for use below script. 

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

    //Type appropriate comment here, and begin script below
    var user = g_form.getreference('requested_for', function(user) {// map the correct field
        g_form.setValue('u_email', user.email.toString());// map the correct field
    });
}

 

if you want to auto-populate the email id based on logged-in user the please line in the default value of the email id field

javascript: gs.getuser().getRecord().getValue('email);

The code contains some typo's. Did you actually test this?

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Murthy Ch
Giga Sage

@RAJJYOTI 

 

If you want to do via code you can try the code which is provided by @BALAJI

else

If you want to do via OOB future where no code required.

then you can follow this article which is written by @Mark Roethof 

https://community.servicenow.com/community?id=community_article&sys_id=49deac8ddb92a010ab0202d5ca961967

 

Let me know if still have any concerns

 

Thanks,

Murthy

 

Thanks,
Murthy

Harneet Sital
Mega Sage
Mega Sage

Hi, 

Here is the solution to this -

Create a new client callable script include with following function 

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

getUserInfo: function() {
var details = {};
var userId = this.getParameter('sysparm_user_id');
var userObj = new GlideRecord('sys_user');
userObj.addQuery('sys_id', userId);
userObj.query();
if (userObj.next()) {
details.email= userObj.email_id.toString();
}
return JSON.stringify(details);
},

type: 'UserDetails'

});

 

Create a new catalog client script on the record producer/request form

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

var ajax = new GlideAjax('UserDetails');
ajax.addParam('sysparm_name', 'getUserInfo');
ajax.addParam('sysparm_user_id', g_form.getValue('employee_name')); // change variable name here
ajax.getXML(doSomething);

function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var answers = JSON.parse(answer);
g_form.setValue('var_email_id', answers.email.toString()); // change variable name here
}
}