Skip to content

Conversation

@vhudlikar
Copy link
Contributor

feat: Excel Attachment Parse to JSON #147

This Flow Designer Action accepts the sys_id of an attachment file and converts its multi-sheet data into a structured JSON string. This enables easy record creation, transformation, and integration downstream in any Flow.


Action Inputs

Name Type Mandatory Description
Attachment SysID String Yes The sys_id of the sys_attachment record containing the Excel file to be parsed.

Logic

The core logic is contained within a single Script Step named Parse Attachment Content. The script prioritizes robustness by first inspecting the file's extension and MIME type.

  1. Validation & Retrieval: It uses a GlideRecord lookup on sys_attachment to validate the sys_id and retrieve the file name and content type.
  2. Parser Selection:
    • For True xlsx Files: The script utilizes the native, server-side sn_impex.GlideExcelParser API. It obtains the file stream via GlideSysAttachment.getContentStream().
    • For csv Files: The script detects the text-based nature and uses GlideSysAttachment.getContent() to retrieve the raw string, followed by custom JavaScript parsing (splitting lines and commas) to extract the data.
  3. Multi-Sheet Iteration: If an xlsx file is detected, the parser loops through all sheets identified by parser.getSheetNames(). For each sheet, it extracts the column headers (which become JSON keys) and iterates through the data rows.
  4. Output Structuring: The result is compiled into an array where each entry is an object representing a sheet: {{ "SheetName": "...", "Records": [ ... ] }}.
  5. Final Conversion: The final JavaScript array is converted to a string using JSON.stringify() to satisfy the Flow Designer output requirement (jsondatastring).

Action Outputs

Name Type Variable Description
JSON Data String jsonDataString The structured JSON array string containing all sheets and their records.
Message String resultMessage Success or detailed error message indicating the outcome and record count.

Example JSON Data Output Structure (for multi-sheet files):

[
  { 
    "SheetName": "Sheet 1 Name", 
    "Records": [
      { "Header1": "Value1", "Header2": "Value2" }
    ]
  },
  { 
    "SheetName": "Sheet 2 Name", 
    "Records": [ ... ] 
  }
]

Test Cases

The following test cases validate functionality using the provided DemoExcel.xlsx file (which is attached).

Positive Test Case

Input Data Test Goal Expected Output
sys_id of DemoExcel.xlsx (contains 2 sheets/files) Verify multi-sheet parsing and data consolidation. resultMessage: {Success: 5 records parsed across 2 sheets from file: DemoExcel.xlsx}

Expected JSON Data:

image

Negative Test Cases

Input Data Test Goal Expected resultMessage
Invalid sys_id Test handling of a non-existent attachment record. {Error: Attachment not found for sys_id: [invalid sys_id]}
Unsupported File Type Test handing of a non-Excel/non-CSV file (e.g., uploading a .pdf file). {Error: Unsupported file format. File Name: [pdf file name]}
Empty File Test handling of an attached .csv file that is 0 bytes or contains only headers. {Error: CSV file is empty or contains only a header line.}

@github-actions
Copy link

Valid PR for ActionPack

Thank you for your contribution. This PR complies with the CONTRIBUTING.md.
A maintainer will review this shortly. In the meantime, Happy Hacking!

@vhudlikar vhudlikar marked this pull request as draft October 10, 2025 15:26
@ravichandra1998g ravichandra1998g self-assigned this Oct 10, 2025
@vhudlikar vhudlikar marked this pull request as ready for review October 10, 2025 15:27
@vhudlikar
Copy link
Contributor Author

DemoExcel.xlsx

@github-actions
Copy link

Valid PR for ActionPack

Thank you for your contribution. This PR complies with the CONTRIBUTING.md.
A maintainer will review this shortly. In the meantime, Happy Hacking!

Copy link
Contributor

@ravichandra1998g ravichandra1998g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @vhudlikar
Thank you very much for submitting!

Thank you for providing demo sheet and clear explanation.

@ravichandra1998g ravichandra1998g merged commit ffe061f into ServiceNowDevProgram:main Oct 10, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants