Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to upload file from Portal page widget ?

Snehal13
Kilo Sage

Any OOB widget that can be cloned ?

 

I have upload option to upload file that must go into attachment table. how to do so in service portal page widget.

8 REPLIES 8

Hi Snehal,

After cloning the widget did you provide ID for that? Please provide the id in lowercase. and then go to your portal configuration and open with the widget designer. I have cloned of Portal Config Widget and used the upload option from there. Pls refer screenshot attached

Which iD ? Here is  what i am trying to do

 

Upload will insert file in attachment table and script will create a record in contract table.

Later on, I am going to update this new record in contract table as per document uploaded ref

Snehal13_0-1707727653624.png

 

Snehal13
Kilo Sage

I cloned but still the widget isnt appearing on my page. From Portal page designer, I have added the widget.

 

Also how to save the uploaded files to attachment table.

Snehal13
Kilo Sage

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+'&amp;table_sys_id='+c.data.rec_sysid+'&amp;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");
			});

		});
	}
};