Autofill an Excel Form with RITM data from Business Rule

Hm10
Tera Contributor

I have an excel file with a form like structure/design. 

I want it to be autofilled from RITM records. and add as attachment and send a notification to someone.

Is this possible through business rule?

2 REPLIES 2

Iraj Shaikh
Mega Sage
Mega Sage

Hi @Hm10 

Yes, it is possible to achieve this functionality in ServiceNow through a combination of business rules, server-side scripting, and possibly integration with external systems if necessary. Here's a high-level overview of how you might approach this:

1. Extract Data from RITM Records: You would need to write a server-side script to extract the data from the Requested Item (RITM) records that you want to use to autofill the Excel form.

2. Generate the Excel File: ServiceNow does not have native support for directly manipulating Excel files, so you would need to use a server-side script to generate the Excel file. This could be done using a third-party library that can be integrated with ServiceNow, such as Apache POI for Java, or by sending the data to an external service that generates the Excel file and returns it to ServiceNow.

3. Attach the Excel File: Once the Excel file is generated, you can attach it to the appropriate record using ServiceNow's attachment API.

4. Send a Notification: You can then use ServiceNow's notification system to send an email to the desired recipient, including the newly attached Excel file as an attachment.

Here's a rough outline of what the business rule might look like:

 

(function executeRule(current, previous /*null when async*/) {

    // Step 1: Extract data from RITM records
    var ritmData = {}; // Populate this object with data from the RITM record

    // Step 2: Generate the Excel file
    // This step may require calling an external service or using a third-party library
    var excelFile = generateExcelFile(ritmData); // This is a placeholder for the actual implementation

    // Step 3: Attach the Excel file to the record
    if (excelFile) {
        var attachment = new GlideSysAttachment();
        attachment.write(current, 'filename.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', excelFile);
    }

    // Step 4: Send a notification
    // You can use an event or the notification system directly to send the email
    gs.eventQueue('your_event_name', current, 'email_recipient', 'email_sender');

})(current, previous);

 


Please note that the above code is a simplified example and does not include the actual implementation details for generating the Excel file or the specifics of your ServiceNow instance. You will need to adapt this to fit your exact requirements and ensure that you have the necessary libraries or external services in place to handle Excel file generation.

Additionally, you may need to consider the performance implications of generating Excel files and attaching them to records, especially if this is expected to happen frequently or for a large number of records at once. It might be more efficient to handle this process asynchronously or via a scheduled job, depending on your use case.

Please mark this response as correct or helpful if it assisted you with your question.

Hm10
Tera Contributor

Is this possible if I upload the excel file with the form layout in sys_attachment and then run business rule that will edit this file with relevant fields for the current RITM and then save this attachment to this RITM?