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.

Follow the steps below or download the solutions file, see Creating Solutions for details.

Setup

  1. Create a new Envelope form script deployment.

  2. Enter a Filename and click SAVE. The extension ‘.js’ is automatically appended if not supplied.

  3. Click on the script link to launch the Scripting Studio.

  4. (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.

    Scripting Studio tools and settings panel showing the form association, the user event association and the selected 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(", "));
    }
}