Custom Field for Store Invoice Number on Time Record when an Invoice is Created for the Billable Time

Requirement

Time Transactions are created through Transactions > Employees > Track Time for billable time to a Customer or Project. When the time is Invoiced, a custom field holding the Invoice Number should be automatically populated on the Time Tacking record.

Solution

Create a Transaction Line Field on the Time Tracking Record with Type = List/Record of TransactionApplied To = Time, Display Type = Disabled

Create a User Event Script deployed on Invoice with After Submit Function that stamps the Custom Transaction Line Field value with the created Invoice Number. The function will retrieve the current Invoice Number and for each of the selected Time Tracking records in the sublist will update the Custom Transaction Line Field holding the Invoice Number

function UpdateTimeTracking(context) {
    //get the current invoice record    
    currentRecordId = context.newRecord.id

    var currentRec = record.load({
        type: record.Type.INVOICE,
        id: currentRecordId,
        isDynamic: false,
    });

    // Get the number of line Time Tracking items submitted 
    let lines = currentRec.getLineCount({ sublistId: 'time' });
    //parse the list of time records
    for (var i = 1; i <= lines; i++) {
        //get the ID of the Time Tracking 
        let timeRecId = objRecord.getSublistValue({
            sublistId: 'time',
            fieldId: 'doc',
            line: i
        });

        let timeSelected = objRecord.getSublistValue({
            sublistId: 'time',
            fieldId: 'apply',
            line: i
        });

        //if it's selected on the invoice, update its custom field
        if (timeSelected == 'T') {
            record.submitFields({
                type: record.Type.TIME_ENTRY,
                id: timeRecId,
                values: {
                    custcol_invoice: currentRecordId
                },
            });
        }
        else {
            //ensure that updates on invoices when Time Tracking records are unapplied
            var timeRecord = nlapiLoadRecord('timebill', timeRecId);

            var timeRecord = record.load({
                type: record.Type.TIME_ENTRY,
                id: timeRecId,
                isDynamic: false,
            });

            var invoiceNoSet = timeRecord.getValue({ fieldId: 'custcol_invoice' });
            if (invoiceNoSet != null)
                record.submitFields({
                    type: record.Type.TIME_ENTRY,
                    id: timeRecId,
                    values: {
                        custcol_invoice: null
                    },
                });
        }
    }
} 

Leave a comment

Your email address will not be published. Required fields are marked *