23maverick23
2/8/2017 - 8:53 PM

NS: Create Project from Opportunity (Workflow Action)

NS: Create Project from Opportunity (Workflow Action)

/**
 * Copyright NetSuite, Inc. 2016 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.
 *
 * Create a basic project header using Opportunity fields.
 *
 * Version    Date            Author           Remarks
 * 1.00       10 Mar 2016     Ryan Morrissey   Initial commit
 *
 */

/**
 * Creates a new project record using default values from the opportunity record.
 * @return {int}    The project record that was created by the script.
 */
function createProjectFromOpp() {
    var ctx         = nlapiGetContext(),
        opportunity = ctx.getSetting('SCRIPT', 'custscript_op_opportunity'),
        company     = ctx.getSetting('SCRIPT', 'custscript_op_company'),
        subsidiary  = ctx.getSetting('SCRIPT', 'custscript_op_subsidiary'),
        title       = ctx.getSetting('SCRIPT', 'custscript_op_title') || 'New project',
        startdate   = ctx.getSetting('SCRIPT', 'custscript_op_startdate'),
        status      = ctx.getSetting('SCRIPT', 'custscript_op_status');

    var project = nlapiCreateRecord('job');
    project.setFieldValue('custentity_opportunity', opportunity);
    project.setFieldValue('parent', company);
    project.setFieldValue('subsidiary', subsidiary);
    project.setFieldValue('companyname', title);
    project.setFieldValue('startdate', startdate);
    project.setFieldValue('entitystatus', status);
    var id = nlapiSubmitRecord(project, true);
    return id;
}