I'm having all sorts of trouble with a Map/Reduce ...
# suitescript
m
I'm having all sorts of trouble with a Map/Reduce SuiteScript to set a value from a custom record on the item line on a transaction. In short, I have 1 key and 3 values that will be used for logic, but I'm not getting the 3 values separated for use. I'm guessing there's a .map that I need since I'm using SuiteQl, but honestly not sure. CODE • In getInputData, I'm using a SuiteQL query that is functioning as expected. • In Map, I'm organizing the line numbers for each transaction. This seems to be working.
Copy code
function map(mapContext) {
    log.audit('map', mapContext);

    var queryValues = JSON.parse(mapContext.value);
    log.debug('queryValues', queryValues); //works

    mapContext.write({
        key: queryValues.values[0],
        value: {
            lineId: queryValues.values[1],
            lineItems: queryValues.values[2],
            unitCost: queryValues.values[3]
            //these all show up in logs in their proper array locations
        }
    });
}
• In Reduce, I'll run the logic, but for reasons I can't find, the array isn't able to separate into each key:value pair. I get key and then the first value is holding all the others, leaving the last 2 as null.
Copy code
//issue: Need to pass key and the other 3 values to call them in transactionline.field logic in reduce

function reduce(reduceContext) {
    var tranId = reduceContext.key;
    var lineId = reduceContext.values[0];
    var lineItems = reduceContext.values[1];
    var unitCost = reduceContext.values[2];

    log.debug({title: 'Tran ID: ', details: tranId});  //works, so I know that 'Key' is successful
    log.debug({title: 'Line ID: ', details: lineId});  //brings array - need to parse/split {"lineId": 1, "lineItems": 299, "unitCost": 24.88}
    log.debug({title: 'Line Items: ', details: lineItems});  //no value
    log.debug({title: 'Unit Cost: ', details: unitCost});  // no value

    //other stuff happens here once I have those values available as objects.
}
When I look for sample codes, they're all either a. only 1 key and 1 value, or b. they use saved search instead of query. That's problematic for me since it's several jumps/JOINs to get the values I need. I'm sure there's an easy answer, but I'm stumped. Thanks!!
a
the reduce context comes in as string, so you need to JSON parse it.
Copy code
var reduceValue = JSON.parse(reduceContext.values);
I think if you just add that line at the start of your reduce... and then update all your references to change
reduceContext.values
to
reduceValue
you should be good.
šŸ‘ 1
m
This worked - Had to facepalm about the string/parse a bit, but had a huge mental block. I now have the variables! Time to do that logic! THANKS!
šŸ‘ 2