23maverick23
10/10/2018 - 8:51 PM

NS: Set invoice on time entry

function afterSumbitUpdateTimeTracking(type) {
    var TIME_TRANSACTION = 'custcol_atc_transaction';
    
    // get the current invoice record    
    var currentRecordId = nlapiGetRecordId();
    var currentRec = nlapiLoadRecord('invoice', currentRecordId);
    
    // get the number of line Time Tracking items submitted
    var lines = currentRec.getLineItemCount('time'); 
    
    //parse the list of time records
    var i = 1;
    for (i; i<=lines; i++) {
        //g et the ID of the Time Tracking 
        var timeRecId = currentRec.getLineItemValue('time', 'doc', i);
        var timeSelected = currentRec.getLineItemValue('time', 'apply', i);
        
        // if it's selected on the invoice, update its custom field
        if (timeSelected == 'T') {
            nlapiSubmitField('timebill', timeRecId, TIME_TRANSACTION, currentRecordId);
        } else {
            // ensure that updates on invoices when Time Tracking records are unapplied
            var timeRecord = nlapiLoadRecord('timebill', timeRecId);
            var invoiceNoSet = timeRecord.getFieldValue(TIME_TRANSACTION);
            if (invoiceNoSet != null) {
                nlapiSubmitField('timebill', timeRecId, TIME_TRANSACTION, null);            
            }
        }
    }
}