Tuesday, June 4, 2019

Search Transaction Related Records in SuiteScript Using 'applyingtransaction'

A common practice when searching for related transactions is through the 'Created From' (createdfrom) field.  Therefore, if Sales Order has associated Item Fulfilment and Cash Sale, search APIs (e.g. nlapiSearchRecord or nlapiCreateSearch) will be called twice before retrieving the desired result:

      var itemFulfillSearch = nlapiSearchRecord('transaction', null,
         [
            new nlobjSearchFilter('Type', null, 'anyof', 'ItemShip'),
            new nlobjSearchFilter('mainline', null, 'is', 'T'),
            new nlobjSearchFilter('createdfrom', null, 'anyof', SALES_ORDER_ID)
         ]
      );

      var cashSaleSearch = nlapiSearchRecord('transaction', null,
         [
            new nlobjSearchFilter('Type', null, 'anyof', 'CashSale'),
            new nlobjSearchFilter('mainline', null, 'is', 'T'),
            new nlobjSearchFilter('createdfrom', null, 'anyof', SALES_ORDER_ID)
         ]
      );

 

The above codes can be shortened by referencing applyingtransaction field on the search column (or criteria):

      var s =
         nlapiSearchRecord('transaction',
            null,
            [
               new nlobjSearchFilter('type', null, 'anyof', 'SalesOrd'),
               new nlobjSearchFilter('internalidnumber', null, 'equalto', '2380')
            ],
            [
               new nlobjSearchColumn('applyingtransaction'),
               new nlobjSearchColumn('recordtype', 'applyingtransaction')
            ]
         );

      for (var i = 0; s != null && i < s.length; i++) {
         var tranId = s[i].getValue('applyingtransaction');
         var tranType = s[i].getValue('recordtype', 'applyingtransaction');

         if ( tranId == '' || tranType == '') continue;

         nlapiLogExecution('DEBUG', tranId + ' / ' + tranType);
      }

This approach avoids redundancy of Search API calls and somehow help reduce governance exceeded errors.

DISCLAIMER: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Netsuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.

Netsuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Netsuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.

Netsuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code

 

No comments:

Post a Comment