HOW TO: Export NetSuite data and log the exported datetime in a custom field

For all NetSuite exports, you can specify an export type of once which will mark a boolean field on the exported NetSuite record after those records are sent from NetSuite and into Celigo. However, in some cases, you may actually want to mark a datetime field and not a boolean/checkbox field. In these cases, you can utilize a preSend NetSuite hook. This hook is a NetSuite SuiteScript record that Celigo will call prior to sending the data from NetSuite to Celigo. The script itself will get the current datetime and update the records for the returned search results.

 

To set this up, follow these steps:

  1. Create your NetSuite export in Celigo with export type = all.
  2. On your saved search in NetSuite, add a filter criteria for _custom_datetime_field is empty.
  3. Add the script below in NetSuite under Documents>Files>SuiteScripts and record the internal id of the script after uploading. NOTE: you will need to update the field id in the script to reflect your actual field id and update the record type if using something other than the customer record type.
  4. Go back to Celigo and add the script function name and script internal id to the NetSuite export preSend script fields.

 

/**
 *@NApiVersion 2.x
 */
define(['N/record', 'N/format'], function(record, format) {
    function customerDateTimeChecker(options) {

        // Get the current date and time
        var currentDate = new Date();

        // Format the date and time for NetSuite
        var formattedDate = format.format({
            value: currentDate,
            type: format.Type.DATETIME
        });

        // List of key-value pair field id and field values to update
        var updateValues = {
            custentity_date_exported: formattedDate
        };

        var response = {};
        response.data = [];
        response.errors = [];
        logAudit('PreSend InputData', JSON.stringify(options));
        for (var i = 0; i < options.data.length; i++) {
            response.data.push(options.data[i]);
        }
        logAudit('PreSend Response', JSON.stringify(response));
        // Update the datetime field for each customer
        for (var i = 0; i < response.data.length; i++) {
            var records = response.data[i];
            var recordId = records.id;

            if (!recordId) {
                response.data[i] = null;
                response.errors.push({
                    code: 'INVALID_ID',
                    message: 'The data does not contain customer id. Please confirm the configuration.'
                });
                continue;
            }
            try {
                record.submitFields({
                    type: record.Type.CUSTOMER,
                    id: recordId,
                    values: updateValues,
                    options: {
                        enablesourcing: true
                    }
                });
            } catch (e) {
                logError('ERROR', 'errorName: ' + e.name + ' & errorMessage: ' + e.message);
                response.data[i] = null;
                response.errors.push({
                    code: e.name,
                    message: e.message
                });
            }
            return response;
        }
    }

    function logError(title, errorMessage) {
        log.error({
            title: title,
            details: errorMessage
        });
    }

    function logAudit(title, details) {
        log.audit({
            title: title,
            details: details
        });
    }
    return {
        customerDateTimeChecker: customerDateTimeChecker
    }
});
0

Comments

0 comments

Please sign in to leave a comment.

 

Didn't find what you were looking for?

New post