Tuesday, June 25, 2019

Ability to email search result from Saved Search in HTML Format using SuiteScript

Customer would like to mimic the behavior of the Email button display on the Search Result screen on the Saved Search where in when clicked the user have the ability to select "Send within Message" and send the search results as HTML format.

In order to achieve this, user can call nlapiSearchRecord to retrieve search results then iterate thru results and place each result in the HTML table as row.

Sample Code Snippet below:

var arrSearchFilters = new Array();
    arrSearchFilters[0] = new nlobjSearchFilter('custrecord_sdr_prod_pref_qty', null, 'greaterthanorequalto', 2);
    arrSearchFilters[1] = new nlobjSearchFilter('subsidiary', 'custrecord_sdr_prod_pref_customer', 'anyof', 1);

    var arrSearchColumns = new Array();
    arrSearchColumns[0] = new nlobjSearchColumn('quantityavailable', 'custrecord_sdr_prod_pref_item');
    arrSearchColumns[1] = new nlobjSearchColumn('subsidiary', 'custrecord_sdr_prod_pref_customer');
    arrSearchColumns[2] = new nlobjSearchColumn('custrecord_sdr_prod_pref_customer');
    arrSearchColumns[3] = new nlobjSearchColumn('custrecord_sdr_prod_pref_item');
    arrSearchColumns[4] = new nlobjSearchColumn('custrecord_sdr_prod_pref_qty');
    arrSearchColumns[5] = new nlobjSearchColumn('email', 'custrecord_sdr_prod_pref_customer');

    var arrSearchResults = nlapiSearchRecord('customrecord_sdr_prod_pref', null, arrSearchFilters, arrSearchColumns);

 var htmlBody = '<body>';
 htmlBody += '<table>';


// Header
 htmlBody += '<tr>';
 htmlBody += ' <td> Number </td>';
 htmlBody += ' <td> Item ID</td>';
 htmlBody += ' <td> Item Name</td>';
 htmlBody += ' <td> Preferred Quantity </td>';
 htmlBody += ' <td> Email address </td>';
 htmlBody += '</tr>';
 
    for ( var i in arrSearchResults) {
        var searchResult = arrSearchResults[i];

        var stQuantityAvailable = searchResult.getValue(arrSearchColumns[0]);
        var stSubsidiaryId = searchResult.getValue(arrSearchColumns[1]);
        var stSubsidiaryName = searchResult.getText(arrSearchColumns[1]);
        var stCustomerId = searchResult.getValue(arrSearchColumns[2]);
        var stCustomerName = searchResult.getText(arrSearchColumns[2]);

        var stItemId = searchResult.getValue('custrecord_sdr_prod_pref_item');
        var stItemName = searchResult.getText('custrecord_sdr_prod_pref_item');
        var stPreferredQty = searchResult.getValue('custrecord_sdr_prod_pref_qty');
        var stEmail = searchResult.getValue('email', 'custrecord_sdr_prod_pref_customer');

        htmlBody += '<tr>';
   htmlBody += ' <td>' + (i+1) + '</td>';
   htmlBody += ' <td>' + stItemId + '</td>';
   htmlBody += ' <td>' + stItemName + '</td>';
   htmlBody += ' <td>' + stPreferredQty + '</td>';
   htmlBody += ' <td>' + stEmail + '</td>';
   htmlBody += '</tr>';  
    }
 
 htmlBody += '</table>';
  htmlBody += '</body>';

 nlapiSendEmail(<internal id of employee> , <email address>, 'Subject', htmlBody, null, null, null);

No comments:

Post a Comment