Update Status field of the task record

Requirement

We need to change the standard status field, which is not displayed on the task form based on the custom status field.

Solution

We can create a user event script to update the standard field for this purpose.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/currentRecord', 'N/record'],
    /**
 * @param{currentRecord} currentRecord
 * @param{record} record
 */
    (currentRecord, record) => {

        /**
         * 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 {
                if (scriptContext.type === 'create' || scriptContext.type === 'edit' || scriptContext.type === 'xedit' || scriptContext.type === 'copy') {
                    var taskRecord = scriptContext.newRecord
                    var taskId = taskRecord.id
                    var custStatus = taskRecord.getValue({
                        fieldId: 'custevent_jj_status'
                    })
                    log.debug('custStatus', custStatus)

                    var stdStatus=taskRecord.getValue({
                        fieldId: 'status'
                    })
                    log.debug('stdStatus', stdStatus)

                    if (custStatus == '1') {
                      record.submitFields({
                            type: record.Type.TASK,
                            id: taskId,
                            values: {
                                status: 'NOTSTART'
                            },
                            options: {
                                enableSourcing: false,
                                ignoreMandatoryFields : true
                            }
                        });



                    } else if ((custStatus == '2') || (custStatus == '3')) {
                        log.debug('In 2')
                        record.submitFields({
                            type: record.Type.TASK,
                            id: taskId,
                            values: {
                                status: 'PROGRESS'
                            },
                            options: {
                                enableSourcing: false,
                                ignoreMandatoryFields : true
                            }
                        });


                    } else if ((custStatus == '4') || (custStatus == '5')) {
                        log.debug('In 3')

                        record.submitFields({
                            type: record.Type.TASK,
                            id: taskId,
                            values: {
                                status: 'COMPLETE'
                            },
                            options: {
                                enableSourcing: false,
                                ignoreMandatoryFields : true
                            }
                        });

                    }
                }
            }
            catch (e) {
                log.debug('Error@Aftersubmit',e)
            }

        }

        return {afterSubmit}

    });

Leave a comment

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