Hi. I have a map reduce script: `function getInput...
# suitescript
v
Hi. I have a map reduce script:
function getInputData() {
var scriptObj = runtime.getCurrentScript();
var filtersParam = runtime
.getCurrentScript()
.getParameter({ name: "custscript_selected_lines_json" });
var lineIdToTransactionIdMap = JSON.parse(filtersParam);
log.debug("lineIdToTransactionIdMap", lineIdToTransactionIdMap);
var searchResults = [];
function createDynamicFilter(keyValuePairs) {
var filters = [];
var index = 0;
for (var internalId in keyValuePairs) {
if (keyValuePairs.hasOwnProperty(internalId)) {
var lineIds = keyValuePairs[internalId];
var internalIdFilter = search.createFilter({
name: "internalid",
operator: search.Operator.ANYOF,
values: [internalId],
});
var lineIdFilters = [];
for (var i = 0; i < lineIds.length; i++) {
var lineIdFilter = search.createFilter({
name: "line",
operator: search.Operator.EQUALTO,
values: [lineIds[i]],
});
lineIdFilters.push(lineIdFilter);
}
log.debug("lineidfilters", lineIdFilters);
if (index > 0) {
filters.push("OR");
}
filters = filters.concat(internalIdFilter, "AND", lineIdFilters);
index++;
}
}
return filters;
}
var keyValuePairs = lineIdToTransactionIdMap;
// Creating the filter
var filters = createDynamicFilter(keyValuePairs);
log.debug("createdynamicfilters", filters);
var isarray = util.isArray(filters);
log.debug("isarray?", isarray);
// Create the search using the filters
var invoiceSearchObj = search.create({
type: "invoice",
filters: [filters],
columns: [
search.createColumn({ name: "internalid", label: "Internal ID" }),
search.createColumn({ name: "trandate", label: "Date" }),
search.createColumn({ name: "entity", label: "Name" }),
search.createColumn({ name: "tranid", label: "Document Number" }),
],
});
return invoiceSearchObj;
}
Returning an error of
Copy code
type: "error.SuiteScriptError",
   name: "WRONG_PARAMETER_TYPE",
   message: "Wrong parameter type: filters is expected as Array. ",
   id: null,
The debug log shows the isarray=true. Why am I getting this error?
b
There are 2 ways to set filters, an array of search.Filter or a filter expression, which is basically an array of arrays of strings
you have chosen an array of arrays of search filters/filter expression
which is just confusing the search constructor
v
I have adjusted it by removing the [] around the filters.However, I am still getting the same error
On the UI, this is what it should look like: filters: [ ["type","anyof","CustInvc"], "AND", ["internalid","anyof","44762"], "AND", [["line","equalto","3"],"OR",["line","equalto","4"]], "OR", ["internalid","anyof","44760"], "AND", [["line","equalto","3"],"OR",["line","equalto","4"]] ],
b
that is a proper filter expression, its an array of array of strings and more arrays
that is not what your code is generating
the array you generate is an array of strings and search.Filters, which is still mixing
v
Thanks Raghav. I have that extension. What I am struggling with is converting data that is returned in the format : { 14651: [ "2", "3" ], 14654: [ "2", "3" ], 14660: [ "1", "2" ], 14663: [ "2", "5" ] } into the required format: [ ["type","anyof","CustInvc"], "AND", ["internalid","anyof","44762"], "AND", [["line","equalto","3"],"OR",["line","equalto","4"]], "OR", ["internalid","anyof","44760"], "AND", [["line","equalto","3"],"OR",["line","equalto","4"]] ],
r
I have changed the format slightly to be more appealing, at least to me. But I think it should work
Copy code
Format: 
[
    ["type","anyof","CustInvc"],
    "AND",
    [
        [
            ["internalid","anyof","44762"],
            "AND",
            [
                ["line","equalto","3"],
                "OR",
                ["line","equalto","4"]
            ]
        ], //temp
        "OR",
        [
            ["internalid","anyof","44760"],
            "AND",
            [
                ["line","equalto","3"],
                "OR",
                ["line","equalto","4"]
            ] // subTemp
        ] // temp
    ] //subFilter
] //filter
And the below code will give you the desired result Use keyword data to store your JSON or update the code accordingly.
Copy code
filter = []
filter.push(["type","anyof","CustInvc"])
filter.push("AND")
subFilter=[]
flag=false
Object.keys(data).map(function(each) {         
    if(flag){
        subFilter.push("OR")
    }
    flag=true
    temp=[]
    temp.push(["internalid","anyof",each])
    temp.push("AND")
    subTemp =[]
    for(i=0;i<data[each].length;i++) {
        subTemp.push(["line","equalto",data[each][i]])
        if(i!=data[each].length-1) {
            subTemp.push("OR")
        }
    }
    temp.push(subTemp)
    subFilter.push(temp)
})

console.log(JSON.stringify(filter))
v
thanks Raghav, I will test this and let you know how I go
Thanks Raghav! That seems to be working. really appreciate it
👍 1