how to attachment added to RITM through scheduled script execution.

Raveendra7
Tera Contributor

Hi

 

I have a requirement to attach a report to RITM through a scheduled email script.

 

Thanks in advance

1 REPLY 1

Amit Gujarathi
Giga Sage
Giga Sage

HI @Raveendra7 ,
I trust you are doing great.

  1. Create a Scheduled Script:

    • Go to "System Definition" > "Scheduled Jobs" in ServiceNow.
    • Click on "New" to create a new scheduled script record.
    • Provide a name and description for the script.
    • Set the schedule for when the script should run (e.g., daily, weekly).
    • Specify the script's script type as "Server" and the script language as "JavaScript".
    • Write the script logic to generate the report and attach it to the RITM record.
  2. Query the RITM record:

    • Use the GlideRecord API to query the RITM record based on your requirements.
    • Specify the conditions to identify the specific RITM(s) you want to attach the report to.
  3. Generate the report:

    • Use the appropriate APIs or methods to generate the report in the desired format (e.g., PDF, Excel).
    • Save the report file to a temporary location.
  4. Attach the report to the RITM record:

    • Use the "insert" method of the Attachment API to attach the report file to the RITM record.
    • Set the relevant fields such as the file name, content type, and table reference (RITM sys_id).
  5. Send an email notification:

    • Use the GlideEmail API to send an email notification to the requester or other relevant recipients.
    • Include a message informing them about the attached report.

Here's an example script to help you get started:

// Step 1: Create a Scheduled Script
// Step 2: Query the RITM record
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('sys_id', 'YOUR_RITM_SYS_ID'); // Add any additional conditions if needed
ritmGR.query();

while (ritmGR.next()) {
  // Step 3: Generate the report
  var reportFile = generateReport(); // Implement your report generation logic

  // Step 4: Attach the report to the RITM record
  var attachment = new GlideSysAttachment();
  attachment.write(ritmGR, 'report.pdf', 'application/pdf', reportFile); // Change the file name and content type as needed

  // Step 5: Send an email notification
  var email = new GlideEmailOutbound();
  email.setSubject('Report Attached to RITM');
  email.setBody('Dear requester, please find the attached report.');
  email.addAttachment(reportFile);
  email.send(ritmGR.variables.email.value); // Change the recipient as needed
}

function generateReport() {
  // Implement your report generation logic and save the file to a temporary location
  return '/path/to/report.pdf';
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi