Hi all, I'm having problems with accessing the val...
# suitescript
h
Hi all, I'm having problems with accessing the values in this array of objects structure. [    {       "item":{          "arstaArticleid":"4012",          "sixNumberedArticleid":null,          "newArticleSelectOption":"2822",          "newBasePrice":null       }    },    {       "item":{          "arstaArticleid":"4013",          "sixNumberedArticleid":"2825",          "newArticleSelectOption":"2825",          "newBasePrice":null       }    },    {       "item":{          "arstaArticleid":"4014",          "sixNumberedArticleid":"3753",          "newArticleSelectOption":"3753",          "newBasePrice":null       }    } ] Does anyone have an idea how to do this?
w
var x = yourobject; x[0].item.arstaArticleid
s
What do you mean having trouble, array[index].item[propName] should do it
h
I have tried that, it doesn't work
w
Where and how have you tested that?
Works pretty good in the console...
h
In a map/reduce script in the map stage inside a custom function
w
I think you need to post your code or a sample of it.
h
I get error "sixNumberedArticleid" is not defined.
w
then I guess it doesn't exist in that scope in the code.
someone else better than me can correct me, but you can't access objects that are created in another stage unless they are passed with the ... hmm can't recall the name of it. The only object you pass between stages.
h
This is what I send to the map() stage from my getInputData:
Copy code
[
   {
      row: {
         customerId: 1228,
         sixNumArticleid: 2812,
         arstaPricingGroup: 15,
         arstaArticleBasePrice: 10.7,
         priceLevel: 25,
         discount: -0.05,
         userInput: "[{"item":{"arstaArticleid":"4012","sixNumberedArticleid":null,"newArticleSelectOption":"2822","newBasePrice":null}},{"item":{"arstaArticleid":"4013","sixNumberedArticleid":"2825","newArticleSelectOption":"2825","newBasePrice":null}},{"item":{"arstaArticleid":"4014","sixNumberedArticleid":"3753","newArticleSelectOption":"3753","newBasePrice":null}}]",
         recid: "131"
      }
   },
   {
      row: {
         customerId: 1228,
         sixNumArticleid: 2825,
         arstaPricingGroup: 23,
         arstaArticleBasePrice: 6.3,
         priceLevel: null,
         discount: null,
         userInput: "[{"item":{"arstaArticleid":"4012","sixNumberedArticleid":null,"newArticleSelectOption":"2822","newBasePrice":null}},{"item":{"arstaArticleid":"4013","sixNumberedArticleid":"2825","newArticleSelectOption":"2825","newBasePrice":null}},{"item":{"arstaArticleid":"4014","sixNumberedArticleid":"3753","newArticleSelectOption":"3753","newBasePrice":null}}]",
         recid: "131"
      }
   },
   {
      row: {
         customerId: 1228,
         sixNumArticleid: 3593,
         arstaPricingGroup: 6,
         arstaArticleBasePrice: 12.5,
         priceLevel: 10,
         discount: -0.1,
         userInput: "[{"item":{"arstaArticleid":"4012","sixNumberedArticleid":null,"newArticleSelectOption":"2822","newBasePrice":null}},{"item":{"arstaArticleid":"4013","sixNumberedArticleid":"2825","newArticleSelectOption":"2825","newBasePrice":null}},{"item":{"arstaArticleid":"4014","sixNumberedArticleid":"3753","newArticleSelectOption":"3753","newBasePrice":null}}]",
         recid: "131"
      }
   },
   {
      row: {
         customerId: 1228,
         sixNumArticleid: 3753,
         arstaPricingGroup: 15,
         arstaArticleBasePrice: 16.6,
         priceLevel: 25,
         discount: -0.05,
         userInput: "[{"item":{"arstaArticleid":"4012","sixNumberedArticleid":null,"newArticleSelectOption":"2822","newBasePrice":null}},{"item":{"arstaArticleid":"4013","sixNumberedArticleid":"2825","newArticleSelectOption":"2825","newBasePrice":null}},{"item":{"arstaArticleid":"4014","sixNumberedArticleid":"3753","newArticleSelectOption":"3753","newBasePrice":null}}]",
         recid: "131"
      }
   }
]
I'm then save the user input in a variable and trying to access it with x[0].item.arstaArticleid
b
My guess is that you didnt parse the value from the map context and its actually a string
w
And the better one has arrived... 🙂
ok, but userInput is a string
so maybe you need to
Copy code
var x = JSON.parse(JSON.parse(context.value).row.userInput);
h
thanks, I'm gonna try that
s
just the inner parse should do
h
I have tried to parse it in getInputData before I parse the return object in getInputData. Then the first thing I do in map is to parse the context.value... haha, three parsing's should be one to many? I added the user input and recid from another search to the row object so I can work on the userInput data in the map stage. But maybe this is not "allowed"
b
Code required at this point
s
In almost every map/reduce, the first thing I do in the map stage is
var results = JSON.parse(context.value)
, I would then log the
results
, and it should be readable
h
alright, when I log the parsed result in map(): var data = JSON.parse(context.value); I get [object Object] but I can retrieve data from the data object. here is my code for getInputData. I create my own object structure in getMappedObjectFromQuery but I suspect the map() stage dosen't like this structure?
Copy code
function getInputData() {
    try {
        //Get UserInput record data 
        var userInputObj = getUserInput() //search
        var result = userInputObj.run().getRange({start: 0, end: 1});
        var isMonday = result[0].getValue({name: "custrecord_nem_arstalistan_is_monday"});
        var isThursday = result[0].getValue({name: "custrecord_nem_arstalistan_is_thursday"});
        var userInput = result[0].getValue({name: "custrecord_nem_arstalistan_json_obj"});
        var recid = result[0].id;

        //Get Customers based on what day to update price list. 
        var query;
        if(isMonday) {
            log.debug('isMonday: ', isMonday);
            query = getSQLquery('mo')
        } else if (isThursday ){
            log.debug('isThursday: ', isThursday);
            query = getSQLquery('thur')
        }

        var sqlQueryResultSet = runSQLquery(query);
        //create mapped to get result length 
        var mapped = sqlQueryResultSet.asMappedResults();
        var length = mapped.length;

        var obj = getMappedObjectFromQuery(sqlQueryResultSet, userInput, length, recid)
        log.debug('obj', obj);

        return obj
    } catch (e) {
        log.error('getInputData error: ', e.message);
    }
}
        
        
        
        
        
        function getMappedObjectFromQuery(sqlQueryResultSet, userInput, length, recid) {
            var sqlQueryResult = sqlQueryResultSet.results
            JSON.stringify(userInput);
            var resultObjectArray = [];
            for (var i = 0; i < length; i++) {
                var customerId = sqlQueryResult[i].values[0];
                var sixNumArticleid = sqlQueryResult[i].values[1];
                var arstaPricingGroup = sqlQueryResult[i].values[2];
                var arstaArticleBasePrice = sqlQueryResult[i].values[3];
                var priceLevel = sqlQueryResult[i].values[4];
                var discount = sqlQueryResult[i].values[5];
    
                var obj = {
                    customerId: customerId,
                    sixNumArticleid: sixNumArticleid,
                    arstaPricingGroup: arstaPricingGroup,
                    arstaArticleBasePrice: arstaArticleBasePrice,
                    priceLevel: priceLevel,
                    discount: discount,
                    userInput: userInput,
                    recid: recid
                }
                var mapreduceobj = {
                    row: obj
                }
                resultObjectArray.push(mapreduceobj);
            }
            return resultObjectArray
        }




        function getSQLquery(deliveryDay) {
            var query = "SELECT " +
                "customer.id, " +
                "customeritempricing.item, " +
                "item_sexsiffrig.custitem_nem_item_pricegr_arsta, " +
                "baseprice_fyrsiffrig.unitprice, " +
                "customergrouppricing.level, " +
                "pricelevel.discountpct, " +

                " FROM customer " +

                "JOIN customeritempricing on customer.id=customeritempricing.customer " +
                "JOIN item as item_sexsiffrig on customeritempricing.item=item_sexsiffrig.id AND customeritempricing.customer=customer.id " +
                "LEFT OUTER JOIN customergrouppricing on customergrouppricing.group=item_sexsiffrig.custitem_nem_item_pricegr_arsta AND customergrouppricing.customer=customer.id AND customergrouppricing.level <> 1 " +
                "JOIN item as item_fyrsiffrig on item_sexsiffrig.id=item_fyrsiffrig.custitem_nem_item_arsta_item " +
                "JOIN pricing as baseprice_fyrsiffrig on baseprice_fyrsiffrig.item=item_fyrsiffrig.id " +
                "LEFT OUTER JOIN pricelevel on customergrouppricing.level=pricelevel.id " +

                "WHERE customer.parent is null and customer.custentity_nem_cus_priceupd_" + deliveryDay +"='T' " +
                "AND item_sexsiffrig.custitem_nem_item_arsta_item is not null " +
                "AND baseprice_fyrsiffrig.pricelevel=1 "
            return query
        }
b
map function too
Copy code
function getInputData() {
  return [
    {
      row: {
        customerId: 1228,
        sixNumArticleid: 2812,
        arstaPricingGroup: 15,
        arstaArticleBasePrice: 10.7,
        priceLevel: 25,
        discount: -0.05,
        userInput:
          '[{"item":{"arstaArticleid":"4012","sixNumberedArticleid":null,"newArticleSelectOption":"2822","newBasePrice":null}},{"item":{"arstaArticleid":"4013","sixNumberedArticleid":"2825","newArticleSelectOption":"2825","newBasePrice":null}},{"item":{"arstaArticleid":"4014","sixNumberedArticleid":"3753","newArticleSelectOption":"3753","newBasePrice":null}}]',
        recid: "131",
      },
    },
    {
      row: {
        customerId: 1228,
        sixNumArticleid: 2825,
        arstaPricingGroup: 23,
        arstaArticleBasePrice: 6.3,
        priceLevel: null,
        discount: null,
        userInput:
          '[{"item":{"arstaArticleid":"4012","sixNumberedArticleid":null,"newArticleSelectOption":"2822","newBasePrice":null}},{"item":{"arstaArticleid":"4013","sixNumberedArticleid":"2825","newArticleSelectOption":"2825","newBasePrice":null}},{"item":{"arstaArticleid":"4014","sixNumberedArticleid":"3753","newArticleSelectOption":"3753","newBasePrice":null}}]',
        recid: "131",
      },
    },
    {
      row: {
        customerId: 1228,
        sixNumArticleid: 3593,
        arstaPricingGroup: 6,
        arstaArticleBasePrice: 12.5,
        priceLevel: 10,
        discount: -0.1,
        userInput:
          '[{"item":{"arstaArticleid":"4012","sixNumberedArticleid":null,"newArticleSelectOption":"2822","newBasePrice":null}},{"item":{"arstaArticleid":"4013","sixNumberedArticleid":"2825","newArticleSelectOption":"2825","newBasePrice":null}},{"item":{"arstaArticleid":"4014","sixNumberedArticleid":"3753","newArticleSelectOption":"3753","newBasePrice":null}}]',
        recid: "131",
      },
    },
    {
      row: {
        customerId: 1228,
        sixNumArticleid: 3753,
        arstaPricingGroup: 15,
        arstaArticleBasePrice: 16.6,
        priceLevel: 25,
        discount: -0.05,
        userInput:
          '[{"item":{"arstaArticleid":"4012","sixNumberedArticleid":null,"newArticleSelectOption":"2822","newBasePrice":null}},{"item":{"arstaArticleid":"4013","sixNumberedArticleid":"2825","newArticleSelectOption":"2825","newBasePrice":null}},{"item":{"arstaArticleid":"4014","sixNumberedArticleid":"3753","newArticleSelectOption":"3753","newBasePrice":null}}]',
        recid: "131",
      },
    },
  ];
}
keep in mind that if i try recreating this, im gonna use a simplification of your getInputData:
h
I manage to get the results, tanks for all the help!
w
So what was the issue?
h
I had to parse userInput in getInputData before sending the obj to the map stage. Then parse the result the first thing I do in map stage.
👍 1