|
1 | | -# Service Portal Widget - Button "Add attachments" |
| 1 | +# Service Portal Widget - "Add attachments" form |
2 | 2 |
|
3 | 3 | This widget is using the 'Attachment API - POST /now/attachment/file' to upload multiple files on form submit. |
4 | 4 |
|
5 | | -## Demo |
6 | | -For demo, update the instance name on the api call statement. |
7 | | - |
8 | | -## Body |
9 | | -<div> |
10 | | -<br/> Table Name: <sn-record-picker table="'sys_db_object'" display-field="'label'" value-field="'name'" field="tableName" search-fields="'label'" default-query="''" required></sn-record-picker> |
11 | | -<br/> Record Number (eg: INC0000055): <input type='text' ng-model='record' ng-change='getID(record)' required/> |
12 | | -<br/><br/> File: <input type="file" id="fileToUpload" multiple onchange="angular.element(this).scope().setFiles(this)"/> |
13 | | -<br/> <div ng-show="files.length"> |
14 | | - <div ng-repeat="file in files.slice(0)"> |
15 | | - <span>{{file.webkitRelativePath || file.name}}</span> |
16 | | - (<span ng-switch="file.size > 1024*1024"> |
17 | | - <span ng-switch-when="true">{{file.size / 1024 / 1024 | number:2}} MB</span> |
18 | | - <span ng-switch-default>{{file.size / 1024 | number:2}} kB</span> |
19 | | - </span>)<span class="glyphicon glyphicon-remove-circle" id="removeicon" ng-click="removeFiles(file)"></span> |
20 | | - </div> |
21 | | - <input type="button" ng-click="uploadFiles()" value="Upload" /> |
22 | | - </div> |
23 | | -</div> |
24 | | - |
25 | | -## Controler As (css) |
26 | | - |
27 | | -#removeicon:hover{ |
28 | | - cursor:pointer; |
29 | | - cursor:hand; |
30 | | -} |
31 | | - |
32 | | -## Client script |
33 | | -function($scope, $http) { |
34 | | - var c = this; |
35 | | -// CODE FOR sn-record-picker |
36 | | - $scope.tableName = { |
37 | | - name: 'tableName' |
38 | | - }; |
39 | | - $scope.$on("field.change", function(evt, parms) { |
40 | | - if (parms.field.name == 'tableName'){ |
41 | | - c.data.table = parms.newValue.toString(); |
42 | | - c.server.update(); |
43 | | - } |
44 | | - }); |
45 | | - |
46 | | -// CODE FOR input record |
47 | | - $scope.getID = function(rec) { |
48 | | - c.data.record = rec.toString(); |
49 | | - c.server.update(); |
50 | | - } |
51 | | - |
52 | | -// CODE FOR fileupload |
53 | | - $scope.files = []; |
54 | | - $scope.setFiles = function(element) { |
55 | | - $scope.$apply(function() { |
56 | | - console.log('files:', element.files); |
57 | | - // Turn the FileList object into an Array |
58 | | - for (var i = 0; i < element.files.length; i++) { |
59 | | - $scope.files.push(element.files[i]); |
60 | | - } |
61 | | - }); |
62 | | - }; |
63 | | - |
64 | | - $scope.removeFiles = function(fname) { |
65 | | - var index = $scope.files.indexOf(fname); |
66 | | - if(index>-1) |
67 | | - $scope.files.splice(index,1); |
68 | | - }; |
69 | | - |
70 | | - $scope.uploadFiles = function() { |
71 | | - $scope.fd = new FormData(); |
72 | | - $scope.files.forEach(function(file){ |
73 | | - $scope.fd.set('files', file); |
74 | | - var request = { |
75 | | - method: 'POST', |
76 | | - url: 'https://<instance-name-here>.service-now.com/api/now/attachment/file?table_name='+c.data.table+'&table_sys_id='+c.data.rec_sysid+'&file_name='+file.name, |
77 | | - data: $scope.fd.get('files'), |
78 | | - headers: { |
79 | | - 'Content-Type': file.type, |
80 | | - 'Accept':'application/json' |
81 | | - } |
82 | | - }; |
83 | | - console.log('HTTP request:',request); |
84 | | - |
85 | | - // SEND THE FILES. |
86 | | - $http(request) |
87 | | - .success(function (d) { |
88 | | - // On success code here |
89 | | - }) |
90 | | - .error(function (err) { |
91 | | - // On error code here |
92 | | - }); |
93 | | - |
94 | | - }); |
95 | | - } |
96 | | - |
97 | | -} |
98 | | - |
99 | | -## Server side |
100 | | - |
101 | | -(function() { |
102 | | - data.response = ''; |
103 | | - if(input.record){ |
104 | | - var gr = new GlideRecord(input.table); |
105 | | - gr.addQuery('number',input.record); |
106 | | - gr.query(); |
107 | | - if(gr.next()){ |
108 | | - data.rec_sysid = gr.sys_id.toString(); |
109 | | - data.response = 'success'; |
110 | | - } |
111 | | - } |
112 | | - |
113 | | -})(); |
114 | | - |
| 5 | +The result will appear in the console or check directly the record. |
0 commit comments