Update a custom field automatically on time record when an invoice is created for Billable time

Requirement

We need to update a custom field on time record, when an invoice is created for Billable time.

Solution

To achieve this result:
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

Following is the sample code for this.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */

define(['N/currentRecord', 'N/record', 'N/redirect', 'N/search'],
    /**
     * @param{currentRecord} currentRecord
     * @param{record} record
     * @param{redirect} redirect
     * @param{search} search
     */
    (currentRecord, record, redirect, search) => {


        /**
         * Defines the function definition that is executed after record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {
            try {

                var currentRec = scriptContext.newRecord
                //Get the record id
                var recId = currentRec.id
              

                //Load the invoice record
                var invRecord = record.load({
                    type: record.Type.INVOICE,
                    id: recId
                })

                //Get the line count of time
                var getLineCount = currentRec.getLineCount({sublistId: 'time'})
                

                for (var i = 0; i < 3; i++) {
                    //Get the value of the field doc
                    var timeRecId = currentRec.getSublistValue({
                        sublistId: 'time',
                        fieldId: 'doc',
                        line: i
                    })
                    //Get the value from field apply
                    var timeSelected=currentRec.getSublistValue({
                        sublistId: 'time',
                        fieldId: 'apply',
                        line: i
                    })
                   

                    //if the time selected value is true, the following code executes
                    if(timeSelected===true){
                        //Set invoice id value to the newly created custom field
                        var t1Id = record.submitFields({
                            type: record.Type.TIME_BILL,
                            id: timeRecId,
                            values: {
                                custcol_jj_inv_id:currentRec.id

                            },
                            options: {
                                enableSourcing: false,
                                ignoreMandatoryFields : true
                            }
                        });
                    }
                    else{
                        //Load the time bill record with the id timeRecId
                        var timeRec=record.load({
                            type: record.Type.TIME_BILL,
                            id:timeRecId
                        })

                        //Get the value of the custom field invoice id
                        var invoiceNoSet=timeRec.getValue('custcol_jj_inv_id')

                        //if the custom field is not empty, set it as null value
                        if(invoiceNoSet!=null){
                            var t2ID = record.submitFields({
                                type: record.Type.TIME_BILL,
                                id: timeRecId,
                                values: {
                                    custcol_jj_inv_id:null

                                },
                                options: {
                                    enableSourcing: false,
                                    ignoreMandatoryFields : true
                                }
                            });
                        }

                    }
                }
            } catch (e) {
                log.debug('Eroor@AfterSubmit', e)
            }
        }

        return {afterSubmit}

    });

Leave a comment

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