Need help with using Document template script

muktha1
Tera Guru

Hi all ,

 

We have a document block created and a document block content created for a particular table with some filter conditions.  In the HTML body we want to call a document template script.I have not used it before and hence have some doubts.

 

We want to do some calculations based on the fields of the table used in the Document Block. That should apply for all the records filtered in the condition field of the Document Block content .Can current be used in the document template script in a Glide Record Query of the table? 

 

Thanks

3 REPLIES 3

Shoheb_IbaaBoss
Tera Guru

Hi,

Try below approach:

 

<!DOCTYPE html>
<html>
<head>
<title>Document Template</title>
</head>
<body>
<h1>Document Template</h1>

<!-- Your HTML content here -->

<script>
// Assuming your document template is associated with a particular table, let's say 'incident'
var gr = new GlideRecord('incident');

// Apply a filter based on conditions from the Document Block Content
gr.addQuery('your_condition_field', 'your_condition_value');
gr.addQuery('additional_condition_field', 'additional_condition_value');
// Add more conditions as needed

gr.query();

while (gr.next()) {
// Access fields and perform calculations
var fieldValue = gr.getValue('your_field_name');
// Perform your calculations here based on the field values

// Output the calculated values or use them as needed in your template
document.write('<p>Calculated value for ' + gr.getValue('number') + ': ' + calculatedValue + '</p>');
}
</script>
</body>
</html>

 

Regards,

Shoheb

muktha1
Tera Guru

Hi Shoheb,

 

Thanks for your reply. So, do you mean to say that all the filter conditions pertaining to the table selected given in the document block content record have to be given in the document template script also? I wanted to know whether like in an email script where we  current refers to the current record of the notification, we have anything here which will refer to the records filtered in the block content.

 

Thanks

If you want to achieve this via email script, try below code:

 

// Assuming your document template is associated with a particular table, let's say 'incident'
var gr = new GlideRecord('incident');

// Apply a filter based on conditions from the Document Block Content
gr.addQuery('your_condition_field', 'your_condition_value');
gr.addQuery('additional_condition_field', 'additional_condition_value');
// Add more conditions as needed

gr.query();

var emailBody = '<!DOCTYPE html>' +
'<html>' +
'<head>' +
'<title>Document Template</title>' +
'</head>' +
'<body>' +
'<h1>Document Template</h1>';

while (gr.next()) {
// Access fields and perform calculations
var fieldValue = gr.getValue('your_field_name');
// Perform your calculations here based on the field values
var calculatedValue = fieldValue * 2; // Replace with your actual calculation logic

// Output the calculated values or use them as needed in your template
emailBody += '<p>Calculated value for ' + gr.getValue('number') + ': ' + calculatedValue + '</p>';
}

emailBody += '</body>' +
'</html>';

// Use 'email' object to send the email with the generated HTML content
email.addMessage(emailBody);
email.send();

Regards,

Shoheb