23maverick23
7/22/2014 - 1:44 PM

NSOA: Create new resource request header

NSOA: Create new resource request header

function createNewResourceRequestHeader(type) {
    try {
        var newProjRec = NSOA.form.getNewRecord();

        if (newProjRec && newProjRec.prj_staffing_plan__c == '1') {
            var myself = NSOA.wsapi.whoami();
            var resReqRec = new NSOA.record.oaResourceRequest();
            resReqRec.number = newProjRec.prj_autoid__c;
            resReqRec.ownerid = (myself && myself.id) ? myself.id : '2';
            resReqRec.customerid = newProjRec.customerid;
            resReqRec.projectid = newProjRec.id;
            resReqRec.date_start = newProjRec.start_date;
            resReqRec.date_start_expected = newProjRec.start_date;
            resReqRec.date_end = newProjRec.finish_date;
            resReqRec.date_finalized = '0000-00-00 0:0:0'; // this seems like a bug to get around e.code 831
            resReqRec.booking_type_id = '3';
            resReqRec.notes = 'Created automatically for this project via user scripting.';
            resReqRec.name = 'Staffing plan for ' + newProjRec.name;
            resReqRec.status = 'O';

            // disable the current user's filter set while script runs
            NSOA.wsapi.disableFilterSet(true);

            // add the new customer po to the project
            var resReqResults = NSOA.wsapi.add(
                [resReqRec]
            );

            if (!resReqResults || !resReqResults[0]) {
                NSOA.meta.log('error', 'Unexpected error! Resource request header was not created.');
            } else if (resReqResults[0].errors) {
                resReqResults[0].errors.forEach(function(err) {
                    NSOA.meta.log('error', 'Error: ' + err.code + ' - ' + err.comment);
                });
            }
        }

        var updProjRec = new NSOA.record.oaProject(newProjRec.id);
        updProjRec.prj_staffing_plan__c = '';
        updProjRec.prj_staffing_plan_number__c = newProjRec.prj_autoid__c;

        // disable the current user's filter set while script runs
        NSOA.wsapi.disableFilterSet(true);

        // enable custom field editing
        var update_custom = {
            name: 'update_custom',
            value: '1'
        };

        // update the project to clear quick customer po create information
        var projResults = NSOA.wsapi.modify(
            [update_custom],
            [updProjRec]
        );

        if (!projResults || !projResults[0]) {
            NSOA.meta.log('error', 'Unexpected error! Project was not updated.');
        } else if (projResults[0].errors) {
            projResults[0].errors.forEach(function(err) {
                NSOA.meta.log('error', 'Error: ' + err.code + ' - ' + err.comment);
            });
        }

    } catch(e) {
        NSOA.meta.log('error', 'Try/catch error: ' + e);
    }
}