Search code examples
javascriptnetsuitesuitescript2.0

Suitescript search changing from a var to a function


I am comfortable with programming searches in Suitescript as a variable. I am trying to convert to a function with "push" capability - but my javascript is failing..

Here is original code:

   var invoiceSearchObj = search.create({
   type: "invoice",
   filters:
   [
      ["item.type","anyof","Kit"], 
      "AND", 
      ["trandate","within","01/01/2023"], 
      "AND", 
      ["type","anyof","CustInvc"], 
      "AND", 
      ["item.custitemitemoriginal","is","T"], 
      "AND", 
      ["status","anyof","CustInvc:B"]
   ],
   columns:
   [
      search.createColumn({
         name: "tranid",
         summary: "MAX",
         sort: search.Sort.ASC
      }),
      search.createColumn({
         name: "formulatext",
         summary: "MAX",
         formula: "{item.memberitem}"
      }),
      search.createColumn({
         name: "internalid",
         join: "createdFrom",
         summary: "MAX"
      })
   ]
});

Here is where I am at with a function:

function invoiceSearchObj() {
    
    /* {N/search.Filter} */
        var filters = [];
        
        /* {N/search.Column} */
        var columns = [];
    
    filterParams = 
      [
        {name: 'item.type', operator: search.Operator.ANYOF, values: 'Kit/Package'}, 
        {name: 'trandate', operator: search.Operator.WITHIN, values: '01/01/2023'},         
        {name: 'item.custitemitemoriginal', operator: search.Operator.IS, values: 'T'}, 
        {name: 'status', operator: search.Operator.ANYOF, values: 'CustInvc:B'}
        ];
   
   /* Add each filter to the search */
        filterParams.forEach(function(params){
            filters.push(search.createFilter(params));
        });
        
    columns.push(search.createColumn({name: 'tranid', summary: search.Summary.MAX, sort: search.Sort.ASC}));    
    columns.push(search.createColumn({name: 'formulatext', summary: search.Summary.MAX, formula: '{item.memberitem}'}));        
    columns.push(search.createColumn({name: 'internalid', join: 'createdFrom', summary: search.Summary.MAX}));  
      
 
   
   return search.create({type: search.Type.INVOICE, filters: filters, columns: columns});
    }

I am getting errors starting with the item.type (tried Kit and Kit/Package) - not sure if this is my coding or the variables I am using.. Thanks in advance!


Solution

  • You should utilize the join property when doing joins to another record for filters.

    filterParams =
      [
        { name: 'type', join: 'item', operator: search.Operator.ANYOF, values: 'Kit/Package' },
        { name: 'trandate', operator: search.Operator.WITHIN, values: '01/01/2023' },
        { name: 'custitemitemoriginal', join: 'item', operator: search.Operator.IS, values: 'T' },
        { name: 'status', operator: search.Operator.ANYOF, values: 'CustInvc:B' }
      ];