2022-07-23

Netsuite: Suitescript 2.X: I need help using searches/getValue to populate custom CPN automatically. Cross referencing item and customer to find CPN

Because Netsuite's native CPN does not let people use spaces in the CPN my company made a custom suitescript to use a custom record type for the CPN. The script below is used to cross reference the customer and the item to generate a list of possible CPNs then it chooses the first and only option in that list. At first we had it routed to the child company, but now we think it might be a better idea to have them connected to the parent company.This way if a company has 12 child companies we only have to upload the CPN one time. Would someone review the code below and let me know why I cant get the code to use the parent customer instead of the child? Or rather it wont populate at all.

define(["N/search", "N/log"], function(Search, _Log) {
    var TAG = "pi_cs_so_v2";

    function Log() {};
    Log.log = function (tag, msg) {
        console.log(tag + " : " + msg);
    };
    Log.debug = function(tag, msg) {
        _Log.debug(tag, msg);
        Log.log(tag, msg);
    };
    Log.error = function(tag, msg) {
        _Log.error(tag, msg);
        Log.log(tag, msg);
    };

    function fieldChanged(context) {
        PartNumber.fieldChanged(context);
    }

    /**
     * Static object, contains customizations relevant to EURO-3
     * @constructor
     */
    function PartNumber () {}
    function cusParent (){}
    /**
     * Handle the native field changed NetSuite call
     * @param context
     * @returns {boolean} True if CPN is updated, else false
     */
    PartNumber.fieldChanged = function(context) {
        var nr = context.currentRecord;
        if (context.sublistId !== "item" || context.fieldId !== "item") return false;
        Log.debug(TAG, "fieldChanged, executing CPN extension");

        var item = nr.getCurrentSublistValue({sublistId: context.sublistId, fieldId: "item"});
        var customer = nr.getValue("entity");
        var parent = nr.getValue({
                        join: "entity",
                        fieldId: "parent",
                        name: "name"
                    });

        Log.debug(TAG, "Item, customer: " + JSON.stringify([item, parent]));
        if (!parent || parent === "" || !item || item === "") return false;
        var cpn = PartNumber.find(parent, item);
        if (!cpn) return false;

        nr.setCurrentSublistValue({sublistId: context.sublistId, fieldId: "custcol_cpn_transaction", value: cpn});
      nr.setCurrentSublistValue({sublistId: context.sublistId, fieldId: "custcol24", value: parent});
        Log.debug(TAG, "Found CPN: " + cpn);
        return true;
    };
    /**
     * Search for the customer part number, assumes there is only ever a single result
     * @param customer
     * @param item
     * @returns {number | undefined} InternalID of the True Customer Part Number record
     */

  PartNumber.find = function(customer, item) {
        var searchObj = Search.create({
            type: "customrecord_true_cpn",
            filters:
                [
                    ["custrecord_cpn_customer","anyof",customer],
                    "AND",
                    ["custrecord_cpn_item","anyof",item]
                ],
            columns: []
        });
        var ans = -1;
        searchObj.run().each(function(result){
            // .run().each has a limit of 4,000 results
            ans = result.id;
            return false;
        });
        return ans !== -1 ? ans : undefined;
    };
 

    return {
        postSourcing: fieldChanged,
    };
});


No comments:

Post a Comment