Options
- 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:
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');
gs.info('Script Include: templateId=' + templateId);
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 new JSON().encode(templateData);
}
gs.info('Script Include: Email template not found for ID=' + templateId);
return new JSON().encode({});
},
type: 'GetEmailTemplateData'
});
This is the client script:
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) {
var responseText = response.responseXML.documentElement.textContent;
console.log('Raw response text: ', responseText);
var emailTemplate = JSON.parse(responseText);
console.log('Parsed emailTemplate:', emailTemplate);
gForm.setValue('u_email_body', emailTemplate.body || '');
gForm.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);
}
});
}
Solved! Go to Solution.
Labels:
- Labels:
-
Human Resources Service Delivery