Issue : Uncertainty in populating sublist values

Issue

As per the requirement, the project task needs to be auto populated in Timesheet only when a project is selected and the project has only one project task. Client script field change was used the in scripts, but found an uncertainty in populating the project tasks. The project tasks populated correctly except in few cases.

Solution

The issue of uncertainty in populating was resolved by using post sourcing entrypoint in netsuite. When the fieldchange was used, project tasks was not always populated correctly. Resolved the issue by using the post sourcing for the same. Following is the code snippet used as a solution.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */

define(['N/currentRecord', 'N/record', 'N/runtime', 'N/search', "N/https", 'N/url'],
    /**
     * @param{currentRecord} currentRecord
     * @param{record} record
     * @param{runtime} runtime
     * @param{search} search
     * @param{https} https
     * @param{url} url

     */
    function (currentRecord, record, runtime, search, https, url) {
        /**
         * Function to be executed when field is slaved.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.currentRecord - Current form record
         * @param {string} scriptContext.sublistId - Sublist name
         * @param {string} scriptContext.fieldId - Field name
         *
         * @since 2015.2
         */
        function postSourcing(scriptContext) {
            try{

                if (scriptContext.sublistId === 'timeitem') {
                    //When field id is customer
                    if (scriptContext.fieldId === 'customer') {
                        //Get current record
                        var currentRec = scriptContext.currentRecord
                
                        //Get current customer id
                        var custId = currentRec.getCurrentSublistValue({
                            sublistId: 'timeitem',
                            fieldId: 'customer',
                            //  line:line

                        })


                        if(checkForParameter(custId)) {
                            var output = url.resolveScript({
                                    scriptId: 796,
                                    deploymentId: 'customdeploy_jj_sl_prjt_tsk_ppltn_aq3339',
                                    returnExternalUrl: false,
                                    params: {
                                        'custscriptcust_id': custId,
                                    }
                                }
                            )

                            https.post.promise({
                                url: output,
                                body: 'Body',
                                headers: {"Content-Type": "application/json"}
                            })
                                .then(function (response) {
                                    var body = JSON.parse(response.body)
                                    var taskId = body.values[0].values["projectTask.internalid"][0].value
                                    
                                    log.debug({

                                        title: 'Response',

                                        details: body

                                    })
                                    //Set current Sublist value as the taskid
                                    currentRec.setCurrentSublistValue({
                                        sublistId: 'timeitem',
                                        fieldId: 'casetaskevent',
                                        value: taskId
                                    })
                                });
                        }
                    }
                }
            }
           catch (e) {
               log.debug('Error@Postsourcing',e)
           }
        }

        function checkForParameter(parameter) {
            if (parameter !== "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== " " && parameter !== 'false') {
                return true;
            }
        }

        return {

            postSourcing:postSourcing

        };

    });

Leave a comment

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