How to fetch a file from the local drive (C or D drive of a computer) and attach to cat item for ATF
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 09:53 AM - edited 10-26-2023 10:03 AM
Hi
I am trying to write a script to fetch a downloaded file from the local drive (C or D drive of a computer) and attach to cat item to raise a request automation for ATF test case?
// ServiceNow Script for fetching a file from the local drive and attaching it to a catalog item
(function () {
// Define parameters
var localFilePath = 'C:\\path\\to\\your\\file.txt'; // Update the path to your local file
var catalogItemSysID = 'your_catalog_item_sys_id'; // Update with the actual catalog item sys_id
var attachmentTableName = 'sc_cart_item'; // Table name for attachments
// Fetch the current user's user ID
var userID = gs.getUserID();
// Create a GlideRecord for the catalog item
var catalogItem = new GlideRecord('sc_cart_item');
if (catalogItem.get(catalogItemSysID)) {
// Read the file
var file = new GlideSysAttachment();
var fileContents = file.read(localFilePath);
// Attach the file to the catalog item
var attachment = new GlideSysAttachment();
var attachmentSysID = attachment.write(catalogItemSysID, 'sc_cart_item', 'file.txt', 'text/plain', fileContents, userID);
gs.log('Attachment Sys ID: ' + attachmentSysID);
} else {
gs.log('Catalog item not found.');
}
})();
this is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 10:23 AM
Hi @Rahul Raja Sami ,
Hope you are doing great.
you'd need a user interaction, like a file input, to select the file. Once the file is selected, you can then use the ServiceNow API to attach it to a record.
Here's a basic outline of how you can achieve this:
- Create a UI Page with a file input for users to select the file.
- Use the Attachment API to attach the selected file to the desired record.
sample script for the UI Page:
<!DOCTYPE html>
<html>
<head>
<title>Attach File</title>
<script src="/scripts/lib/jquery/jquery_latest.js"></script>
</head>
<body>
<input type="file" id="fileInput" />
<button onclick="uploadFile()">Upload</button>
<script>
function uploadFile() {
var fileInput = document.getElementById('fileInput');
var file = fileInput.files[0];
var tableName = 'sc_cat_item'; // Change this to your table name
var recordSysId = 'YOUR_SYS_ID'; // Change this to the sys_id of the record you want to attach to
var formData = new FormData();
formData.append('table_name', tableName);
formData.append('table_sys_id', recordSysId);
formData.append('uploadFile', file);
$.ajax({
url: '/api/now/attachment/upload',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(response) {
alert('File uploaded successfully!');
},
error: function(error) {
alert('Error uploading file: ' + error.responseText);
}
});
}
</script>
</body>
</html>
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 11:33 PM
Why don't you use following steps:
Regards
Shaqeel
***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.
***********************************************************************************************************************
Regards
Shaqeel