- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hello!
I created a custom portal widget that allows a user to download a document residing on a task, upload that document after they download it, and reject the document task if necessary - essentially our custom usage of document tasks. It is working except for the uploading document piece - currently it looks like it is attaching in the widget, but does not actually attach to the document task record. I have a Script Includes that closes out the task within the button and that is fine - but I can't figure out why the upload part won't work. I also haven't been able to successfully re-enable the upload button when an attachment exists- the button will stay disabled with my current html script for some reason. Finally, I'm not sure why the remove attachment won't show - I am in admin so I don't think its an ACL at this point.
Here are my scripts if you could take a look! I am sure your giant brains can see what I am doing wrong! 🙂
Thanks in advance!
HTML:
<!-- List Mode -->
<div ng-if="c.data.mode === 'list'" class="doc-task-list">
<!-- Active Tasks -->
<h4 ng-click="c.toggle('active')" class="collapsible-header">
<span ng-class="{'icon-vcr-right': !c.show.active, 'icon-vcr-down': c.show.active}"></span>
Active Tasks <span class="badge">{{c.data.tasks.active.length}}</span>
</h4>
<div ng-show="c.show.active">
<div ng-if="c.data.tasks.active.length === 0">
<p>No active tasks assigned to you.</p>
</div>
<div ng-repeat="task in c.data.tasks.active" class="task-card">
<h5>{{task.short_description}}</h5>
<p>Task: {{task.number}} | Parent: {{task.parent}}</p>
<p>State: <span class="state-badge active">{{task.state_display}}</span></p>
<!-- File upload input -->
<div class="task-card">
SAAR Attachment:
<input type="file" multiple
onchange="angular.element(this).scope().setFiles(this, '{{task.sys_id}}')" />
<div ng-show="taskFiles[task.sys_id].length">
<div ng-repeat="file in taskFiles[task.sys_id]">
<span>{{file.name}}</span>
( <span ng-switch="file.size > 1024*1024">
<span ng-switch-when="true">{{file.size / 1024 / 1024 | number:2}} MB</span>
<span ng-switch-default>{{file.size / 1024 | number:2}} kB</span>
</span> )
<span class="glyphicon glyphicon-remove-circle"
ng-click="removeFiles(file, task.sys_id)"></span>
</div>
</div>
</div>
<!-- Action buttons -->
<div class="action-buttons">
<button class="btn btn-primary" ng-click="c.downloadLatest(task.sys_id)">
Download SAAR Attachment for Signature
</button>
<button class="btn btn-success"
ng-click="uploadFiles(task.sys_id); c.completeTask(task.sys_id)"
ng-disabled="!taskFiles[task.sys_id] || !taskFiles[task.sys_id].length">
Upload PDF and Complete SAAR Signature
</button>
<button class="btn btn-danger" ng-click="c.rejectTask(task.sys_id)">
Reject SAAR
</button>
</div>
</div>
</div>
<!-- Completed Tasks -->
<h4 ng-click="c.toggle('completed')" class="collapsible-header">
<span ng-class="{'icon-vcr-right': !c.show.completed, 'icon-vcr-down': c.show.completed}"></span>
Completed Tasks <span class="badge">{{c.data.tasks.completed.length}}</span>
</h4>
<div ng-show="c.show.completed">
<div ng-if="c.data.tasks.completed.length === 0">
<p>No completed tasks.</p>
</div>
<div ng-repeat="task in c.data.tasks.completed" class="task-card completed">
<h5>{{task.short_description}}</h5>
<p>Task: {{task.number}} | Parent: {{task.parent}}</p>
<p>State: <span class="state-badge completed">{{task.state_display}}</span></p>
</div>
</div>
</div>
CSS:
.doc-task-list {
display: flex;
flex-direction: column;
gap: 20px;
}
.task-card {
padding: 15px;
border: 1px solid #d3d6dc;
border-radius: 6px;
background: #fff;
}
.task-card.completed {
background: #f4f4f4;
color: #888;
}
.action-buttons {
display: flex;
gap: 10px;
margin-top: 10px;
}
.badge {
display: inline-block;
min-width: 20px;
padding: 3px 8px;
font-size: 12px;
font-weight: 600;
color: #fff;
background-color: #5b9bd5;
border-radius: 10px;
vertical-align: middle;
margin-left: 6px;
}
.state-badge {
display: inline-block;
padding: 2px 8px;
font-size: 12px;
font-weight: 600;
border-radius: 12px;
color: #fff;
}
.state-badge.active {
background-color: #0078d4;
}
.state-badge.completed {
background-color: #4caf50;
}
.state-badge.rejected {
background-color: #d9534f;
}
.state-badge.in-progress {
background-color: #f0ad4e;
}
.collapsible-header {
cursor: pointer;
display: flex;
align-items: center;
font-weight: 600;
margin: 15px 0 10px;
}
.collapsible-header span {
margin-right: 6px;
}
Server Script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago - last edited 4 weeks ago
Hi @kristenmkar ,
The script isn't working because of the onchange on your input. I don't think it's doing what you think it's doing.
Once you used vanillajs or the browser api "onchange" you stepped out of the AngularJS realm. I know in your code you make up for it with the $apply and scope however that's not applied on the onchange itself. So the sys_id is not actually being captured.
<input type="file" multiple
onchange="angular.element(this).scope().setFiles(this, '{{task.sys_id}}')" />
In this scenario '{{task.sys_id}}' will be passing '{{task.sys_id}}' as the value, not the actual sys_id.
Just like the angular class was used to get to the "setFiles" method you'll need to do the same thing to get the task.sys_id.
Try doing this:
<input type="file" multiple
onchange="angular.element(this).scope().setFiles(this, angular.element(this).scope().task.sys_id)" />
Edit:
Other ways to pass/get the task sys_id:
<input type="file" multiple
onchange="angular.element(this).scope().setFiles(this)" ng-attr-task_sys_id="{{task.sys_id}}" />
And in the setFiles method get the value of the attribute "tasksysid" value
$scope.setFiles = function(element) {
var sys_id = element.attributes.tasksysid.value;
$scope.$apply(function() {
if (!$scope.taskFiles[sys_id]) {
$scope.taskFiles[sys_id] = [];
}
for (var i = 0; i < element.files.length; i++) {
$scope.taskFiles[sys_id].push(element.files[i]);
}
});
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi @kristenmkar ,
Ensure table_name and table_sys_id are correct and passed to the attachment REST call...In the upload REST URL (/api/now/attachment/file?...), make sure table_sys_id=sys_id matches the record you want.....Double check c.data.table equals "sn_doc_task" or whichever table you want.....
Modify your onchange input to get the actual sys_id properly....Instead of onchange="angular.element(this).scope().setFiles(this, '{{task.sys_id}}')", use something like onchange="angular.element(this).scope().setFiles(this, task.sys_id)" if that's valid, or store task.sys_id in an attribute and retrieve it in the handler....Ensure the Angular digest ($scope.$apply) is triggered correctly.
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
For clarity:
1) Is the "complete" button the button with text "Upload PDF and Complete SAAR Signature"
2) Does "at least 1 attachment added" mean a file uploaded before actually attaching to the record or does that mean at least 1 attachment attached to the record?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
I apologize for the lack of clarification! I was hoping that I could disable that button until they attached something within the widget - so there would at least be some file uploaded before actually attaching to the record. Essentially, we don't want a user completing the task/hitting that button without actually attaching something. However, now I think I may also be confusing myself. I have the Complete (Upload PDF and Complete SAAR Signature) button action doing this - c.completeTask(task.sys_id), which is the below:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
What you have is working, or should be working. I replicated your code in a PDI and as far as files being uploaded or even removed it does disable the button. The only change I made was the table and of course fixing the sys_id issue.
Other than that, I've made no changes from the code you posted and the button gets disabled when no files have been uploaded.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
I forgot to add. In the script include, in case something went wrong with attaching, you could probably put a check to make sure the file(s) actually get attached to the record before updating the record to complete.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Oh my gosh, I forgot to add back my disablement code for the button. Awesome, it works! Would you mind sharing the code you used to fix the sys_id issue? It seems like my changes worked but I'm finding that it's not always attaching? Wondering if it could be a scope issue.
I really appreciate all your help, thank you!!