```function getInputData() { try { v...
# suitescript
k
Copy code
function getInputData() {

    try {

        var script = runtime.getCurrentScript();

        var schoolId = script.getParameter({name: 'custscript_school_id'})
        var schoolName = script.getParameter({name: 'custscript_school_name_param'})

        var uniqueID = Math.floor(Math.random() * (Math.random() * 20) + 100) + 5

        var anotherValue = uniqueID + 44

        var array1 = []
        var array2 = []
        var array3 = []


        var customSearch = search.create({
            type: search.Type.CUSTOMER,
            title: 'custsearch_' + anotherValue,
            id: 'customsearch_customer_' + uniqueID,
            isPublic: true,
            filters:
                ['custentity_lf_school_record', 'is', schoolId],
            columns:
                [
                    search.createColumn({
                        name: "entityid",
                        sort: search.Sort.ASC,
                        label: "Name"
                    })

                ]
        });

        var assemblyItemSearch = search.create({
            type: search.Type.ASSEMBLY_ITEM,
            title: 'custsearch_' + anotherValue,
            id: 'customsearch_assembly_' + uniqueID,
            isPublic: true,
            filters: [
                ["type", "anyof", "Assembly"],
                "AND",
                ["isinactive", "any", ""],
                "AND",
                ['custitem_lf_school', 'is', schoolId],
                "AND",
                ['custitem_lf_school_name', 'is', schoolName],
            ],
            columns:
                [
                    search.createColumn({
                        name: "itemid",
                        sort: search.Sort.ASC,
                        label: "Name"
                    })
                ]
        })

        var inventoryItemsSearch = search.create({
            type: search.Type.INVENTORY_ITEM,
            title: 'custsearch_' + anotherValue,
            id: 'customsearch_inventoryItem_' + uniqueID,
            isPublic: true,
            filters: [
                ["type", "anyof", "InvtPart"],
                "AND",
                ["isspecialorderitem", "any", ""],
                "AND",
                ['custitem_lf_school', 'is', schoolId],
                "AND",
                ['custitem_lf_school_name', 'is', schoolName],
            ],
            columns:
                [
                    search.createColumn({
                        name: "itemid",
                        sort: search.Sort.ASC,
                        label: "Name"
                    })
                ]
        })


        customSearch.run().each(function (result) {
            // .run().each has a limit of 4,000 results
            array1.push(result);

            return true;
        });

        assemblyItemSearch.run().each(function (result) {

            array2.push(result);

            return true;
        })

        inventoryItemsSearch.run().each(function (result) {

            array3.push(result);

            return true;
        })


        return [...array1, ...array2, ...array3]



    } catch (e) {
        log.debug("Error", e)
    }
    
}

/**
 * Defines the function that is executed when the map entry point is triggered. This entry point is triggered automatically
 * when the associated getInputData stage is complete. This function is applied to each key-value pair in the provided
 * context.
 * @param {Object} mapContext - Data collection containing the key-value pairs to process in the map stage. This parameter
 *     is provided automatically based on the results of the getInputData stage.
 * @param {Iterator} mapContext.errors - Serialized errors that were thrown during previous attempts to execute the map
 *     function on the current key-value pair
 * @param {number} mapContext.executionNo - Number of times the map function has been executed on the current key-value
 *     pair
 * @param {boolean} mapContext.isRestarted - Indicates whether the current invocation of this function is the first
 *     invocation (if true, the current invocation is not the first invocation and this function has been restarted)
 * @param {string} mapContext.key - Key to be processed during the map stage
 * @param {string} mapContext.value - Value to be processed during the map stage
 * @since 2015.2
 */

const map = (mapContext) => {

    try {

        // var script = runtime.getCurrentScript();
        // var standardRoy = script.getParameter({name: 'custscript_school_std_roy_perc'})
        // var headwearRoy = script.getParameter({name: 'custscript_lf_school_hdwr_roy_perc'})
        // var specialRoy = script.getParameter({name: 'custscript_lf_school_spec_roy_perc'})




        var listID = JSON.parse(mapContext.value)

      return listID.id





    } catch (e) {
        log.debug("Error Message:", e)
    }


}
c
@Kevin Baxter Do you know how to create Text Snippets in Slack? They are much more compact and don't take up dozens/hundreds of lines in the Slack channel, plus they afford syntax highlighting for easier parsing.
k
map.js
🙌 1
c
If you're creating ephemeral searches that you're not intending to save, you don't need to create names / ids for them, thus relieving you of the need to generate unique IDs etc
You're passing the names of different types of records into map - what's your intent with those records, and how do you intend to differentiate between customers and items?
Anything you do to update records in the map() function, such as record.load or record.submitFields, will require the record's type and internal ID. @Kevin Baxter
k
UpdateMap.js
@Clay Roper I just updated the script to show what I want to do with the mapContext value
Do you think this will work or should I use record.SubmitField
c
Looks ok to me at a glance, although you will also need to call Record.save() after setting the values. loading, setting values, and saving is costlier from a governance unit usage perspective, but that's not a huge concern in a map/reduce like this. One difference to consider between Record.load/save and record.submitFields is that the former is an EDIT context and the latter is an XEDIT context. The fields that show up in
beforeSubmitContext.newRecord
differ significantly between those two contexts, which might trip up field validation in beforeSubmit scripts. Just something to keep an eye on. I assume you'd be updating your getIntputData script to feed the record types and internal IDs? Also, if you move forward with the approach of pushing results to the return value, you can push it all to a single array instead of three different ones that you spread into a return value. You might consider doing something like the following in getInputData
Copy code
resultArray.push({
  id: searchResult.getValue({ "internalid" }),
  type: record.Type.ASSEMBLY_ITEM or record.Type.INVENTORY_ITEM or record.Type.CUSTOMER
})
You won't be able to set fields that start with
custitem
on a Customer record btw
b
ignoring that the code doesnt work for customers
the code for all 3 cases are the same, so you can just use the same code
similarly, ignoring the customer case, you can combine both the item records by doing an item search with an
OR
in the filter expression
im not sure if those item searches are the final product, but if they are, they dont do anything different from each other and can really just be combined as an item search with a type filter
k
I was able to get the script working using the record.submitFields method. Thank you for you help @Clay Roper
🙌 2