Sunday, January 13, 2019

Search System Notes in SuiteScript

System Notes are not editable by default as they are system generated and commonly used for auditing record changes.  However, it could still be searchable in SuiteScript as long as its parent record is supported, based on SuiteScript Records Browser.

This can serve as an alternate solution for Enhancement #168271 (Suitescript > Expose System Notes for scripting).

Note that in SuiteScript, only the records and fields indicated in SuiteScript Records Browser are supported.  Since SuiteScript can call a Saved Search through nlapiLoadSearch (and nlapiSearchRecord), System Notes fields (even though not officially supported at this point) can be referenced by nlobjSearchResult[i].getValue(searchColumns[n]), where searchColumns holds an array of nlobjSearchColumn (nlobjSearchResult[i].getAllColumns()) object.

This article provides a simple walkthrough on how System Notes can be retrieved in SuiteScript.  The example below will search for System Notes of Customer records whose emails had changed.

   1. Navigate to Lists > Search > Saved Searches > New > Customer
   2. Specify the following fields:
           - Title: Customer with System Note
           - ID: _customer_with_system_note (This will be used as the Saved Search reference in the script later)

   3. Define Criteria and Results as shown below and Save (Make sure that it returns some results):


 

   4. Create a Suitelet Script with the following codes:

               function jCustomerSystemNotes(request, response) {
                    var strOutput = '';
                    var htmlBreak = '<br>\n';

                    var systemNotesSearch = nlapiLoadSearch('customer', 'customsearch_customer_with_system_note');

                    // Additional filters may be added as well.  For example it will be for a specific customer only.
                    var filters = new Array();
                    filters.push(new nlobjSearchFilter('internalidnumber', null, 'equalto', '71'));
                    systemNotesSearch.addFilters(filters);

                    var resultSet = systemNotesSearch.runSearch();
                    var totalRecords = 0;

                    resultSet.forEachResult(
                        function (searchResult) {
                           totalRecords++;
                           var searchColumns = resultSet.getColumns();
                           strOutput += 
                              'Name: ' + searchResult.getValue(searchColumns[0]) + htmlBreak +
                              'Type: ' + searchResult.getValue(searchColumns[1]) + htmlBreak +
                              'Field: ' + searchResult.getValue(searchColumns[2]) + htmlBreak +
                              'Old Value: ' + searchResult.getValue(searchColumns[3]) + htmlBreak +
                              'New Value: ' + searchResult.getValue(searchColumns[4]) + htmlBreak +
                              'Date: ' + searchResult.getValue(searchColumns[5]) + htmlBreak +
                              'Set By: ' + searchResult.getValue(searchColumns[6]) + htmlBreak +
                              '--------------------------------------------' + htmlBreak;
                           return true;
                        }
                     );


                    response.write(
                       '<html>\n' +
                       '   <head><title>Customer with System Notes Search</title></head>\n' +
                       '   <body>\n' + 
                        '<p>' + totalRecords + ' results found.</p>\n' +
                        '<p>' + strOutput + '</p>\n' +
                        '   </body>\n' +
                        '</html>\n'
                      );
               }

 

   5. Run the Suitelet and you should have a result like this:


[Saved Search Screenshot Missing]

 

No comments:

Post a Comment