When submitting an expense report, validate each ticket has an attachment (e.g. scanned receipt)
This script validates each receipt has an attachment when submitting an expense report.
-
Verifies whether document attachments exist on a ticket record
-
Does not require an attachment if "Missing receipt" is checked
Note:The Enable the missing paper receipt feature switch needs to be enabled for this option.
Follow the steps below or download the solutions file, see Creating Solutions for details.
Setup
-
Create a new Envelope form script deployment.
-
Enter a Filename and click SAVE. The extension ‘.js’ is automatically appended if not supplied.
-
Click on the script link to launch the Scripting Studio.
-
(1) Copy the Program Listing below into the editor, (2) set the Before approval event, and set check_receipt_has_attachments as the Entrance Function.
Program Listing
function check_receipt_has_attachments(type) {
// return if not an approve_request
if (type != 'approve_request')
return;
// Load receipt data
var envelope = NSOA.form.getOldRecord();
var ticket = new NSOA.record.oaTicket();
ticket.envelopeid = envelope.id;
var readRequest = {
type: "Ticket",
fields: "id, attachmentid, reference_number, missing_receipt",
method: "equal to",
objects: [ticket],
attributes: [{
name: "limit",
value: "250"
}]
};
var arrayOfreadResult = NSOA.wsapi.read(readRequest);
var missingAttachment = [];
if (!arrayOfreadResult || !arrayOfreadResult[0])
NSOA.form.error('', "Internal error reading envelope receipts.");
else if (arrayOfreadResult[0].errors === null && arrayOfreadResult[0].objects)
arrayOfreadResult[0].objects.forEach(
function(o) {
if (o.attachmentid === '0' && o.missing_receipt != '1')
missingAttachment.push(o.reference_number);
}
);
if (missingAttachment.length > 0) {
NSOA.form.error('',
"The following receipts (by reference number) are missing an attachment: " +
missingAttachment.join(", "));
}
}