- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2025 01:25 AM
Hi, I am trying to auto-populate some fields when a specific field(email template) is changed.
When I select an Email Template(references 'email content' table) in an hr task record producer, I want to get the email template's fields: message_html and subject, and auto-fill the form's email body and email subject fields.
This is the script include:
This is the client script:
Solved! Go to Solution.
- Labels:
-
Human Resources Service Delivery
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2025 02:12 AM
update as this, don't use JSON encode
var GetEmailTemplateData = Class.create();
GetEmailTemplateData.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getEmailTemplate: function() {
var templateId = this.getParameter('sysparm_u_email_template');
var gr = new GlideRecord('sn_hr_core_email_content');
gr.addQuery('sys_id', templateId);
gr.query();
if (gr.next()) {
var templateData = {
body: gr.message_html.toString(),
subject: gr.subject.toString(),
};
gs.info('Script Include: body=' + templateData.body);
gs.info('Script Include: subject=' + templateData.subject);
return JSON.stringify(templateData);
}
gs.info('Script Include: Email template not found for ID=' + templateId);
return '';
},
type: 'GetEmailTemplateData'
});
Client script: use g_form
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
console.log('Triggering GlideAjax for sys_id: ' + newValue);
var ga = new GlideAjax('GetEmailTemplateData');
ga.addParam('sysparm_name', 'getEmailTemplate');
ga.addParam('sysparm_u_email_template', newValue);
ga.getXMLAnswer(function(response) {
try {
if (response) {
console.log('Raw response text: ', responseText);
var emailTemplate = JSON.parse(response);
console.log('Parsed emailTemplate:', emailTemplate);
g_form.setValue('u_email_body', emailTemplate.body || '');
g_form.setValue('u_email_subject', emailTemplate.subject || '');
console.log('client script works');
} else {
console.error('Error: No response received from Script Include');
}
} catch (e) {
console.error('Error parsing response: ', e);
}
});
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2025 01:26 AM
the Client script of course works, but fails on calling script include.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2025 01:30 AM
@YounseoC use g_form instead of gForm.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2025 02:12 AM
update as this, don't use JSON encode
var GetEmailTemplateData = Class.create();
GetEmailTemplateData.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getEmailTemplate: function() {
var templateId = this.getParameter('sysparm_u_email_template');
var gr = new GlideRecord('sn_hr_core_email_content');
gr.addQuery('sys_id', templateId);
gr.query();
if (gr.next()) {
var templateData = {
body: gr.message_html.toString(),
subject: gr.subject.toString(),
};
gs.info('Script Include: body=' + templateData.body);
gs.info('Script Include: subject=' + templateData.subject);
return JSON.stringify(templateData);
}
gs.info('Script Include: Email template not found for ID=' + templateId);
return '';
},
type: 'GetEmailTemplateData'
});
Client script: use g_form
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
console.log('Triggering GlideAjax for sys_id: ' + newValue);
var ga = new GlideAjax('GetEmailTemplateData');
ga.addParam('sysparm_name', 'getEmailTemplate');
ga.addParam('sysparm_u_email_template', newValue);
ga.getXMLAnswer(function(response) {
try {
if (response) {
console.log('Raw response text: ', responseText);
var emailTemplate = JSON.parse(response);
console.log('Parsed emailTemplate:', emailTemplate);
g_form.setValue('u_email_body', emailTemplate.body || '');
g_form.setValue('u_email_subject', emailTemplate.subject || '');
console.log('client script works');
} else {
console.error('Error: No response received from Script Include');
}
} catch (e) {
console.error('Error parsing response: ', e);
}
});
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader