Make comments mandatory with attachments in standard converation widget

arshad199110
Tera Contributor
 

arshad199110_1-1705214560049.png

Whenever an attachment is made to the ticket conversation, it automatically attaches the file with client side server update. I would like to stop the attachment and wait for the comments to be added before attaching the file. If anyone has any suggestion on how to implements this. Maybe auto update can be stopped and when attachment is made, it just holds and only when send button is clicked it will submit with the comments and files.
Widget client side script:


$scope.$on("attachment.upload.start", function() {
$scope.data.isPosting = true;
$scope.msg = $scope.data.uploadingAttachmentMsg;
spAriaUtil.sendLiveMessage($scope.msg);
})
 
$scope.$on("attachment.upload.stop", function() {
$scope.data.isPosting = false;
$scope.msg = "";
if (postLock) 
$scope.data.journalEntry = "";
 
//update the stream so we get the new attachment
c.server.update().then(function(r) {
$scope.data.stream = r.stream;
});
});
2 REPLIES 2

Iraj Shaikh
Mega Sage
Mega Sage

Hi @arshad199110 

To achieve the behavior you're looking for, you'll need to modify the client-side script of the ServiceNow widget to intercept the attachment upload process and only proceed with the server update when a comment is present. Below is an example of how you might modify the client-side script to include a check for comments before allowing the attachment to be uploaded.

 

// Flag to check if there is a pending attachment
var pendingAttachment = false;

$scope.$on("attachment.upload.start", function() {
    // Set the flag to true when an attachment upload starts
    pendingAttachment = true;
    $scope.data.isPosting = true;
    $scope.msg = $scope.data.uploadingAttachmentMsg;
    spAriaUtil.sendLiveMessage($scope.msg);
});

$scope.$on("attachment.upload.stop", function() {
    // Set the flag to false when an attachment upload stops
    pendingAttachment = false;
    $scope.data.isPosting = false;
    $scope.msg = "";
    if (postLock) {
        $scope.data.journalEntry = "";
    }
    // Do not update the stream immediately
});

// Modify the send function to check for comments before sending
$scope.send = function() {
    if ($scope.data.journalEntry) {
        // If there is a comment, proceed with the server update
        c.server.update().then(function(r) {
            $scope.data.stream = r.stream;
            // Clear the comment field after sending
            $scope.data.journalEntry = "";
        });
    } else if (pendingAttachment) {
        // If there is a pending attachment but no comment, alert the user
        alert("Please add a comment before attaching a file.");
    }
};

// You may need to bind the modified send function to the send button in your widget's HTML

 


Please note that this is a simplified example and may require additional adjustments based on your specific widget implementation and requirements. You'll need to ensure that the `send` function is properly bound to the send button in your widget's HTML template and that the server script is set up to handle the update correctly.

Additionally, you may want to provide user feedback when they try to attach a file without a comment, such as displaying an error message or disabling the attachment button until a comment is entered. This will help guide users to follow the desired process.

Please mark this response as correct or helpful if it assisted you with your question.

Thanks for your reply Iraj.
I tried adding the lines which you mentioned, but it didn't work. Then I tried to find out at which line of code the attachment upload is happening by comments those lines.  None of the above lines prevent from file being uploaded. This is with the "Standard ticket conversation" widget.