The CreatorCon Call for Content is officially open! Get started here.

How to create a word document with the catalog item variables and attach it to the ticket

Devika3
Tera Guru

Hi All,

We have a requirement to create a word document with the variables from the catalog form and attach it to the ticket created.

Please don't redirect me to any threads as most of them are outdated.

Regards,

Devika.

2 REPLIES 2

Ratnakar7
Mega Sage

Hi @Devika3 ,

 

Creating a Word document with catalog item variables and attaching it to a ticket in ServiceNow involves a multi-step process. Here's a high-level overview of the steps you can follow:

  1. Create a Word Template:

    • Start by creating a Word document template. In this template, you can define the structure of your document, including placeholders for catalog item variables.
  2. Create a Business Rule:

    • Write a server-side Business Rule that triggers when a catalog item is ordered and generates the Word document.
    • In the script, fetch the values of catalog item variables.
    • Use a library like mammoth.js (https://github.com/mwilliamson/mammoth.js/) or a similar tool to fill in the placeholders in your Word template with the variable values.
  3. Generate the Word Document:

    • Utilize a server-side scripting language or external libraries to generate a Word document. This process typically involves replacing placeholders in the Word template with actual data.
  4. Attach the Document to the Ticket:

    • Attach the generated Word document to the respective ticket record. In ServiceNow, you can use the GlideSysAttachment API to create an attachment record and link it to the ticket.

Here's a simplified example of how the code might look in a server-side Business Rule:

// Create and populate the Word document
var wordDoc = // Generate your Word document here

// Create a new attachment record
var attachment = new GlideSysAttachment();
attachment.write(current, "word_document.docx", "application/msword", wordDoc);

// Attach the document to the catalog item (current is the catalog item record)
attachment.setValue("table_name", current.getTableName());
attachment.setValue("table_sys_id", current.sys_id);
attachment.setValue("file_name", "word_document.docx");
attachment.setValue("type", "application/msword");
attachment.insert();

// Optionally, provide feedback to the user
gs.addInfoMessage("Word document generated and attached.");

 

Thanks,

Ratnakar

Hi Ratnakar,

Can you please elaborate on this as i didn't understood most of the information that you have provided.

1. How can we generate the word template?

2. How can we place the catalog variables into the word template?

3. How to generate the word document from the template?

4. How to attach the new word file to the ticket?

5. Are we achieving all of the above through a single script? If so how it could be?

Please guide me on this as we are completely new to this requirement.

 

Regards,

Devika.