23maverick23
3/14/2014 - 2:49 AM

NSOA: Append note to notes field

NSOA: Append note to notes field

/**
 * Copyright NetSuite, Inc. 2014 All rights reserved.
 * The following code is a demo prototype. Due to time constraints of a demo,
 * the code may contain bugs, may not accurately reflect user requirements
 * and may not be the best approach. Actual implementation should not reuse
 * this code without due verification.
 *
 * This script uses a custom field to append notes to the standard
 * notes field on a record. It can be reused for multiple complex
 * record types by modifying the variables at the start of the function.
 *
 * Version    Date            Author           Remarks
 * 1.00       13 Mar 2014     Ryan Morrissey
 *
 */

function autoAppendNotes() {
    try {
        /**
         * Change these variable values to match your record and
         * field names. Anything below this section is complex
         * record type agnostic.
         */

        var RECORD_TYPE = 'Projecttask',                 // change to match record type
            NOTES_FLD = 'notes',                         // change to match standard notes field name
            CUST_NOTES_FLD = 'prj_task_note_append__c',  // change to match custom field name
            OBJ = new NSOA.record.oaProjecttask();       // change to match SOAP complex type


        // --- DO NOT MODIFY BELOW THIS LINE! ---


        var newRec = NSOA.form.getNewRecord(),
            notes = newRec[NOTES_FLD],
            append = newRec[CUST_NOTES_FLD];

        if (!append || append.length == 0) {
            // no note to append - skip
            return;
        }

        var uName = NSOA.wsapi.whoami().name,
            updNotes;

        updNotes = '[' + uName + ' ' + getDateTime() + '] ' + append + '\n' + notes;

        OBJ.id = newRec.id;
        OBJ[NOTES_FLD] = updNotes;
        OBJ[CUST_NOTES_FLD] = '';

        NSOA.wsapi.disableFilterSet(true);

        var res = NSOA.wsapi.modify([ {name: 'update_custom', value: '1'} ], [OBJ]);

        if (!res || !res[0]) {
            NSOA.meta.log('error', 'Modify error for ' + RECORD_TYPE + ' with id ' + newRec.id);
            return;
        } else if (res[0].errors !== null && res[0].errors.length > 0) {
            res[0].errors.forEach(function(e) {
                var error = e.code + ' - ' + e.comment + ' ' + e.text;
                NSOA.meta.log('error', 'Error updating record: ' + error);
            });
            return;
        }

    } catch(e) {
        NSOA.meta.log('fatal', 'Unexpected error: ' + e);
        return;
    }
}

function getDateTime() {
    /**
     * Returns the current date/time stamp in EDT using a standard offset of 4 hours from GMT.
     * The string output is then parsed to display Date and Time
     *
     * @returns {String}: MMM DD YYYY HH:MM:SS (EDT)
     */

    var d = new Date(),
        edt = new Date((d.getTime() + (d.getTimezoneOffset() * 60000)) + (3600000 * -4)),
        re,
        m;

    re = /(\w{3}\s)(\w{3}\s\d{1,2}\s\d{4}\s\d{1,2}\:\d{1,2}\:\d{1,2}\s)(\w{3}(\+|\-)\d{4}\s)(\(\w{3}\))/;
    m = re.exec(edt);
    
    return m[2] + m[5];
}