How to Auto Populate email id of user in email field whose name is specified in Requested For field of a Request form.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-28-2021 08:08 AM
How to Auto Populate email id of user in email field whose name is specified in Requested For field of a Request form.
- Labels:
-
Continual Improvement (CIM)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-28-2021 08:13 AM
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-28-2021 09:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-28-2021 09:14 AM
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
https://community.servicenow.com/community?id=community_article&sys_id=49deac8ddb92a010ab0202d5ca961967
Let me know if still have any concerns
Thanks,
Murthy
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2021 03:42 AM
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
}
}