Populate custom record in oppurtunity record.

Population of the “Contract Health and Safety Form” custom record in the oppurtunity record.

define(["N/search", "N/record","N/currentRecord"],

    /**
     * @param{search} search
     * @param{record} record
     * @param{currentRecord} currentRecord
     */
    (search, record, currentRecord) => {

        /**
         * @description Function to check the paramater and parameter value
         *@param {} parameter
         *@param {} parameterName
         * @returns boolean
         */
        function checkForParameter(parameter, parameterName) {
            if (
                parameter != "" &&
                parameter != null &&
                parameter != undefined &&
                parameter != "null" &&
                parameter != "undefined" &&
                parameter != " " &&
                parameter != false
            ) {
                return true;
            } else {
                if (parameterName)
                    log.debug(
                        "Empty Value found",
                        "Empty Value for parameter " + parameterName
                    );
                return false;
            }
        }
        /**
         * Defines the search for the Oppurtunity record in the subtab
         *
         */
        const isContractorHealthRecordExists = (subContractor) => {
            try {
                var custRec = {};
                var customrecord_jj_contractor_healthSearchObj = search.create({
                    type: "customrecord_jj_cr_safety_form_ncps62",
                    filters: ["custrecord_jj_cr_vendor_ncps62", "anyof", ...subContractor],
                    columns:
                        [
                            search.createColumn({
                                name: "created",
                                sort: search.Sort.ASC,
                                label: "Date Created"
                            }),
                            search.createColumn({ name: "custrecord_jj_cr_vendor_ncps62", label: "Vendor" }),
                            search.createColumn({ name: "custrecord_jj_oppurtunity", label: "Opportunity" }),
                            search.createColumn({ name: "internalid", label: "Internal ID" })
                        ]
                });

                customrecord_jj_contractor_healthSearchObj.run().each(function (result) {
                    var custRecObj = {};
                    var opportunityArray;
                    var vendor = result.getValue({ name: "custrecord_jj_cr_vendor_ncps62", label: "Vendor" });
                    var internalId = result.getValue({ name: "internalid", label: "Internal ID" });
                    var opportunity = result.getValue({ name: "custrecord_jj_oppurtunity", label: "Oppurtunity" });
                    if (checkForParameter(opportunity)) {
                        opportunityArray = opportunity.split(",");
                    }
                    custRecObj.internalId = internalId;
                    if (checkForParameter(opportunityArray)) {
                        custRecObj.opportunity = opportunityArray
                    }
                    custRec[vendor] = custRecObj
                    return true;
                });

                return custRec;
            } catch (er) {
                log.debug("Error@isContractorHealthRecordExists", er)
                return {}
            }
        }
        /**
         * @description No field should be selected in the make a copy context
         * @param {*} scriptContext
         */
        const beforeLoad = (scriptContext) => {
            if (scriptContext.type === scriptContext.UserEventType.COPY) {
                try {
                    const newRecord = scriptContext.newRecord;
                    log.debug("newRecord", newRecord)
                        scriptContext.newRecord.removeSelectOption({
                        fieldId: "custbodysubcontractors",
                        value: null
                    })
                    log.debug("subcontractorField", subcontractorField)
                } catch {
                    log.debug("error@beforeload")
                }
            }
        }


        /**
         *
         *
         * Defines the function definition that is executed after record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {
            try {
                if (scriptContext.type === scriptContext.UserEventType.CREATE || scriptContext.UserEventType.EDIT || scriptContext.UserEventType.COPY) {

                    /**
                     * @description Getting old record values and unset the custom record if it is an edit context
                     */
                    if (scriptContext.type === scriptContext.UserEventType.EDIT) {
                        const oldOpportunityRec = scriptContext.oldRecord;
                        const oppurtunityRecordId = scriptContext.newRecord.id
                        let oldSubContractors = oldOpportunityRec.getValue({
                            fieldId: "custbodysubcontractors"
                        });
                        log.debug("subcontractors",oldSubContractors)
                        if (checkForParameter(oldSubContractors)) {
                            let custRecObj = isContractorHealthRecordExists(oldSubContractors);
                            for (let key in custRecObj) {
                                if (custRecObj.hasOwnProperty(key)) {
                                    var indexOpportunityOld = custRecObj[key]["opportunity"].indexOf(oppurtunityRecordId);
                                    if (indexOpportunityOld > -1) {
                                        custRecObj[key]["opportunity"].splice(indexOpportunityOld, 1);
                                    }
                                    log.debug("custrec",custRecObj[key]["opportunity"])
                                    record.submitFields({
                                        type: 'customrecord_jj_cr_safety_form_ncps62',
                                        id: custRecObj[key]["internalId"],
                                        values: {
                                            "custrecord_jj_oppurtunity": custRecObj[key]["opportunity"]
                                        }
                                    });

                                }
                            }
                        }
                    }
                    /**
                     * @description Defines the record population in the new record
                     */
                    const oppurtunityRecord = scriptContext.newRecord;
                    const oppurtunityRecordId = scriptContext.newRecord.id;
                    let subContractor = oppurtunityRecord.getValue({
                        fieldId: "custbodysubcontractors"
                    });
                    log.debug("subContractor",subContractor)
                    if (checkForParameter(subContractor)) {
                        let custRecordObj = isContractorHealthRecordExists(subContractor);
                        for (let key in custRecordObj) {
                            if (custRecordObj.hasOwnProperty(key)) {
                                var indexOpportunityID = custRecordObj[key]["opportunity"].indexOf(oppurtunityRecordId.toString());
                                if (indexOpportunityID < 0) {
                                    custRecordObj[key]["opportunity"].push(oppurtunityRecordId.toString());
                                }
                                record.submitFields({
                                    type: 'customrecord_jj_cr_safety_form_ncps62',
                                    id: custRecordObj[key]["internalId"],
                                    values: {
                                        "custrecord_jj_oppurtunity": custRecordObj[key]["opportunity"]
                                    }
                                })
                            }
                        }
                    }
                }
            }
            catch (error) {
                log.debug("error@afterSubmit", error)
            }
        }
        return { beforeLoad, afterSubmit }
    });

Leave a comment

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