GlideSysAttachment - Sending as 'undefined'

gcm1
Tera Expert

Hey guys

I need help... I'm using the attachmentHandler in the service portal to use the upload file in a custom component. Everything has worked so far, but when I go to view the attached file in the record, a new tab opens with 'undefined'... what am I missing to be able to upload it correctly?

 

Client controller:

uploader = $element[0].querySelector('#uploader');
		$scope.attachmentHandler = new nowAttachmentHandler(function(attachments, evt) {
		}, function(error) {
			console.log('error', error);
		});

		angular.element(uploader).on('uploaded-files', function(e) {

			$scope.uploadedFiles = e.detail.files;
			if (!$scope.uploadedFiles || Object.keys($scope.uploadedFiles).length === 0) {
				return;
			}
			$scope.files = [];
			for (var fileId in $scope.uploadedFiles) {
				if ($scope.uploadedFiles.hasOwnProperty(fileId)) {
					$scope.files.push($scope.uploadedFiles[fileId]);
				}
			}

			$scope.uploadButtonControl();
			var files = e.detail.files;
			uploader.customAttachmentState = $scope.files
			$scope.uidSelected = $scope.files[0]['uid']
			$scope.nameSelected = $scope.files[0]['name']

			var aux = _.find(uploader.customAttachmentState, function(element) {
				return element.uid === $scope.uidSelected
			});
		});

		$timeout(function(){
			uploader.defaultAttachmentActions.remove($scope.uidSelected)
		}, 19000)

		angular.element(uploader).on('remove-file', function(e) {
		});
	}

  $scope.uploadAttachedFiles = function(){
		$scope.server
			.get({
			action: "upload",
			docAttach: $scope.files,
			recSysId: $scope.data.docCreated
		});
	};

 

if (input.action == 'upload') {
		var docAttach = input.docAttach;
		for (var key in docAttach) {
			if (docAttach.hasOwnProperty(key)) {
				var file = docAttach[key];
				var name = file.name;
				var extension = file.extension;
				var attachmentTable = 'incident';
				var recordSysId = input.recSysId;

				var StringUtil = new GlideStringUtil();
				var attachment = new GlideSysAttachment();

				// Set up inputs
				var rec = new GlideRecord(attachmentTable);
				rec.get(recordSysId);
				var fileName = name;
				var contentTypeX = extension;
				var content = file.content;

				var attachmentSysId = attachment.write(rec, fileName, contentTypeX, content);

				console.log('The attachment sys_id is: ' + attachmentSysId);
			}
		}
	}
2 REPLIES 2

msd93
Kilo Sage

Hi @gcm1 

 

Can you check in attachments table, if any attachment is getting stored. Also could you provide snips/images of testing done.

Hello @msd93 

Thank you for your reply!

 

Sure, I can see the attachment stored on sys_attachment table (and in fact I notice that the file was being uploaded with content type = pdf only, so I tried to fix that, and I am able to see content type = application/pdf now):

 

Screenshot 2023-09-14 at 14.28.22.png

 

and I can see on the record:

 

Screenshot 2023-09-14 at 14.28.40.png

but when I try to open the file, this occurs:

 

Screenshot 2023-09-14 at 14.28.58.png

And this is the steps of my testing

Screenshot 2023-09-14 at 14.33.45.pngScreenshot 2023-09-14 at 14.34.02.png