- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2024 01:59 AM - edited ‎02-12-2024 02:00 AM
From Portal widget, I have file upload option and button to upload the file. Upon click of this button, it must create record in attachment table and attach the file to this new record.
What is wrong in below code ? Unable to upload file to attachment table.
HTML
<div> <br> File: <input type="file" id="fileToUpload" onchange="angular.element(this).scope().setFiles(this)"> <br> <input type="button" ng-click="uploadFiles()" value="Upload"></div>
Server script
(function() { data.response = ''; var gr = new GlideRecord('sys_attachment'); //input.table gr.insert(); //how to upload file during insert })();
Client controller
api.controller=function($scope) { var c = this; // CODE FOR sn-record-picker $scope.tableName = { name: 'sys_attachment' }; $scope.$on("field.change", function(evt, parms) { if (parms.field.name == 'tableName'){ c.data.table = parms.newValue.toString(); c.server.update(); } }); // CODE FOR input record $scope.getID = function(rec) { c.data.record = rec.toString(); c.server.update(); }; // CODE FOR fileupload /*$scope.files = []; $scope.setFiles = function(element) { $scope.$apply(function() { // Turn the FileList object into an Array for (var i = 0; i < element.files.length; i++) { $scope.files.push(element.files[i]); } }); }; $scope.removeFiles = function(fname) { var index = $scope.files.indexOf(fname); if(index>-1) $scope.files.splice(index,1); }; */ $scope.uploadFiles = function() { alert("Trying to upload file"); $scope.fd = new FormData(); $scope.files.forEach(function(file){ $scope.fd.set('files', file); var request = { method: 'POST', url: 'https://dev23367.service-now.com/api/now/attachment/file?table_name='+c.data.table+'&table_sys_id='+c.data.rec_sysid+'&file_name='+file.name, data: $scope.fd.get('files'), headers: { 'Content-Type': file.type, 'Accept':'application/pdf' } }; // SEND THE FILES. $http(request) .success(function (d) { alert("Uploaded with success"); }) .error(function (err) { alert("Error"); }); }); } };
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2024 01:15 AM
The code you provided has several issues that could be causing the file upload to fail. Here are the potential issues and their solutions:
1. Server Script: The server script is creating a new GlideRecord for the 'sys_attachment' table and immediately inserting it. However, there is no data being set on this record before it is inserted. This means that the record will be empty and won't contain any file data.
2. Client Controller: The client controller is trying to upload the file using a POST request to the ServiceNow REST API. However, the FormData object is not being populated correctly. The 'files' field is being set with a single file, but the forEach loop suggests that multiple files could be uploaded at once.
3. URL in POST request: The URL in the POST request is hard-coded to a specific ServiceNow instance ('https://dev23367.service-now.com'). This could cause issues if the instance changes or if the code is moved to a different instance.
4. File Type: The 'Accept' header in the POST request is set to 'application/pdf', which means that only PDF files will be accepted. If you are trying to upload a different type of file, this could be causing the upload to fail.
Here are the steps to correct these issues:
- Server Script: You don't need to create a new GlideRecord in the server script. The file upload will automatically create a new record in the 'sys_attachment' table.
- Client Controller: You should populate the FormData object with all the files that need to be uploaded. You can do this by using the 'append' method instead of the 'set' method.
- URL in POST request: You should use a relative URL for the POST request instead of a hard-coded URL. This will ensure that the request is sent to the correct instance.
- File Type: If you want to upload different types of files, you should remove the 'Accept' header from the POST request or set it to the correct file type.
Here is the corrected code:
Client Controller:
$scope.uploadFiles = function() {
alert("Trying to upload file");
$scope.fd = new FormData();
$scope.files.forEach(function(file){
$scope.fd.append('files', file);
var request = {
method: 'POST',
url: '/api/now/attachment/file?table_name='+c.data.table+'&table_sys_id='+c.data.rec_sysid+'&file_name='+file.name,
data: $scope.fd.get('files'),
headers: {
'Content-Type': file.type
}
};
// SEND THE FILES.
$http(request)
.success(function (d) {
alert("Uploaded with success");
})
.error(function (err) {
alert("Error");
});
});
}
nowKB.com
For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/
For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2024 03:02 AM
What are you trying to do? A record in the attachment table IS the attachment. You can't attach an attachment to an attachment. You need a record to which you attach your file and that attachment is the record in the attachment table.
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2024 03:24 AM
@Mark Manders ,Here is my use case
From Portal widget, I have file upload option and button to upload the file. Upon click of this button, it must create attachment file new record in attachment table.
How to do this ? I am unable to upload file to attachment table.
HTML
<div> <br> File: <input type="file" id="fileToUpload" onchange="angular.element(this).scope().setFiles(this)"> <br> <input type="button" ng-click="uploadFiles()" value="Upload"></div>
Server script
(function() { data.response = ''; var gr = new GlideRecord('sys_attachment'); //input.table gr.insert(); //how to upload file during insert })();
Client controller
api.controller=function($scope) { var c = this; // CODE FOR sn-record-picker $scope.tableName = { name: 'sys_attachment' }; $scope.$on("field.change", function(evt, parms) { if (parms.field.name == 'tableName'){ c.data.table = parms.newValue.toString(); c.server.update(); } }); // CODE FOR input record $scope.getID = function(rec) { c.data.record = rec.toString(); c.server.update(); }; // CODE FOR fileupload /*$scope.files = []; $scope.setFiles = function(element) { $scope.$apply(function() { // Turn the FileList object into an Array for (var i = 0; i < element.files.length; i++) { $scope.files.push(element.files[i]); } }); }; $scope.removeFiles = function(fname) { var index = $scope.files.indexOf(fname); if(index>-1) $scope.files.splice(index,1); }; */ $scope.uploadFiles = function() { alert("Trying to upload file"); $scope.fd = new FormData(); $scope.files.forEach(function(file){ $scope.fd.set('files', file); var request = { method: 'POST', url: 'https://dev23367.service-now.com/api/now/attachment/file?table_name='+c.data.table+'&table_sys_id='+c.data.rec_sysid+'&file_name='+file.name, data: $scope.fd.get('files'), headers: { 'Content-Type': file.type, 'Accept':'application/pdf' } }; // SEND THE FILES. $http(request) .success(function (d) { alert("Uploaded with success"); }) .error(function (err) { alert("Error"); }); }); } };
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2024 11:52 PM
You are uploading a file into the attachment table. Where are you attaching it to? What record are you creating that you attach your document to. That's the attachment table. I only read that you are trying to upload a file, but to what record?
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2024 01:15 AM
The code you provided has several issues that could be causing the file upload to fail. Here are the potential issues and their solutions:
1. Server Script: The server script is creating a new GlideRecord for the 'sys_attachment' table and immediately inserting it. However, there is no data being set on this record before it is inserted. This means that the record will be empty and won't contain any file data.
2. Client Controller: The client controller is trying to upload the file using a POST request to the ServiceNow REST API. However, the FormData object is not being populated correctly. The 'files' field is being set with a single file, but the forEach loop suggests that multiple files could be uploaded at once.
3. URL in POST request: The URL in the POST request is hard-coded to a specific ServiceNow instance ('https://dev23367.service-now.com'). This could cause issues if the instance changes or if the code is moved to a different instance.
4. File Type: The 'Accept' header in the POST request is set to 'application/pdf', which means that only PDF files will be accepted. If you are trying to upload a different type of file, this could be causing the upload to fail.
Here are the steps to correct these issues:
- Server Script: You don't need to create a new GlideRecord in the server script. The file upload will automatically create a new record in the 'sys_attachment' table.
- Client Controller: You should populate the FormData object with all the files that need to be uploaded. You can do this by using the 'append' method instead of the 'set' method.
- URL in POST request: You should use a relative URL for the POST request instead of a hard-coded URL. This will ensure that the request is sent to the correct instance.
- File Type: If you want to upload different types of files, you should remove the 'Accept' header from the POST request or set it to the correct file type.
Here is the corrected code:
Client Controller:
$scope.uploadFiles = function() {
alert("Trying to upload file");
$scope.fd = new FormData();
$scope.files.forEach(function(file){
$scope.fd.append('files', file);
var request = {
method: 'POST',
url: '/api/now/attachment/file?table_name='+c.data.table+'&table_sys_id='+c.data.rec_sysid+'&file_name='+file.name,
data: $scope.fd.get('files'),
headers: {
'Content-Type': file.type
}
};
// SEND THE FILES.
$http(request)
.success(function (d) {
alert("Uploaded with success");
})
.error(function (err) {
alert("Error");
});
});
}
nowKB.com
For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/
For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER