can you produce a map/reduce script that targets p...
# ask-stanley-ai
c
can you produce a map/reduce script that targets po lines using a saved search that creates a key value pair with the internal id of the transactions and the line ids to delete in the PO?
c
AI generated answer (using internal Slack threads):
Yes, you can produce a map/reduce script that targets PO lines using a saved search and creates a key-value pair with the internal id of the transactions and the line ids to delete in the PO. Here's a basic example of how you might structure this script:
Copy code
javascript
define(['N/search', 'N/record', 'N/runtime'], function(search, record, runtime) {
    function getInputData() {
        // Load the saved search
        var mySearch = search.load({
            id: 'customsearch_my_search'
        });
        return mySearch;
    }

    function map(context) {
        var searchResult = JSON.parse(context.value);
        var internalId = searchResult.id;
        var lineId = searchResult.values['line'];

        // Write the internal id and line id to the context
        context.write({
            key: internalId,
            value: lineId
        });
    }

    function reduce(context) {
        var internalId = context.key;
        var lineIds = context.values;

        // Load the record
        var myRecord = record.load({
            type: record.Type.PURCHASE_ORDER,
            id: internalId
        });

        // Loop through the line ids and remove the lines
        lineIds.forEach(function(lineId) {
            myRecord.removeLine({
                sublistId: 'item',
                line: lineId
            });
        });

        // Save the record
        myRecord.save();
    }

    return {
        getInputData: getInputData,
        map: map,
        reduce: reduce
    };
});
This script loads a saved search in the
getInputData
stage, then in the
map
stage it parses the search results and writes the internal id of the transaction and the line id to the context. In the
reduce
stage, it loads the transaction record, removes the lines specified by the line ids, and then saves the record. Please note that this is a simplified example and may need to be adjusted based on your specific requirements and the structure of your saved search. Also, be aware of the governance usage of your script, as large saved searches and record operations can consume significant amounts of governance units. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.851603866
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.838742852
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.838384926
Please provide your feedback on this answer.