Create txt file from saved search and send through workflow action script

Jira Code: APC-14

Created a text file by workflow action script. A scheduled workflow created, on entry, workflow triggers the script and creates the file. On send email action, mail sent to the selected user with the text file as the attachment.

Workflow Action Script:

/**
 * @NApiVersion 2.x
 * @NScriptType workflowactionscript
 */
 /**
 * Script Description
 *  Saved search text file creation. The workflow triggers the script, in script we load a search and create a text file. 
 */
/*******************************************************************************
 * Allied power and control
 * **************************************************************************
 * 
 * Date: 26/03/2019
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 * 
 * REVISION HISTORY
 * 
 * Revision 1 $ 26/03/2019 Maria: Create
 * 
 ******************************************************************************/
define(['N/record', 'N/runtime', 'N/search','N/file'],

function(record, runtime, search, file) {
   
    /**
     * Definition of the Suitelet script trigger point.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.newRecord - New record
     * @param {Record} scriptContext.oldRecord - Old record
     * @Since 2016.1
     */
    function onAction(scriptContext) {

    try{
        var mySearch  = search.load({
            id: 'customsearch_apc_mona_po_ack'
        });

        var cols = mySearch.columns.length;
        // Get search result count

        var searchResultCount = mySearch.runPaged().count;

        var main_array = [];
        var txtfiles = '';           
        
        var singleResult; // temporary variable used to store the result set
        var startIndex = 0;
        var endIndex = 1000;
        do {
            try{

                var searchResult = mySearch.run().getRange({
                    start: startIndex,
                    end: endIndex
                });

                for (var j = 0; j < searchResultCount; j++) {

                    var singleResult = searchResult[j];
                   

                    if (singleResult != null && singleResult != undefined){
                                try{
                                    var txtfile = '';
                                    //To get search result columns
                                    var searchCols = singleResult.columns;


                                        for(var k=0;k<cols;k++){

                                            var label = searchCols[k].label;

                                            if ((singleResult.getValue(searchCols[k]) != null) &&
                                                    (singleResult.getValue(searchCols[k]) != undefined)) {


                                                if(label != "Distributor Item ID"){
                                                   if(k == cols-1){
                                                        txtfile += singleResult.getValue(searchCols[k]);
                                                    }else{
                                                        txtfile += singleResult.getValue(searchCols[k]) + '~';
                                                    }
                                                }
                                                else{
                                                    logme("success");
                                                   if(k == cols-1){
                                                        txtfile += singleResult.getText(searchCols[k]);
                                                    }else{
                                                        txtfile += singleResult.getText(searchCols[k]) + '~';
                                                    }
                                                }                                               
                                                
                                                
                                            }
                                        }
                                        txtfiles += txtfile += '\r\n';

                                } catch(e) {
                                    logme("@thirdtry", getError(e));
                                }
                    }else{
                        logme("no results");
                    }

                }
                startIndex = startIndex + endIndex;

            } catch (e) {
                logme("secondTry", getError(e));
            }
        } while (singleResult.length > 0)

        var today = new Date();
        var dd = today.getDate()+1;
        var mm = today.getMonth()+1;
        var yyyy = today.getFullYear();
        today = mm+'_'+dd+'_'+yyyy;

        var fileObj = file.load({
            id: 13529
        });
        fileObj.name = 'Salesordersearch_'+today+'.txt';
        var fileId = fileObj.save();

        } catch (e) {
            logme("firstTry", getError(e));
        }
    }

    return {
        onAction : onAction
    };

    /*******************************************************************************
         * return error
         * 
         * @param e
         * @returns
         * 
         * Created on 22-Feb-2019 by Maria
         */
        function getError(e) {
            var stErrMsg = '';
            if (e.getDetails != undefined) {
                stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>' +
                    e.getStackTrace();
            } else {
                stErrMsg = '_' + e.toString();
            }
            return stErrMsg;
        }

        /*******************************************************************************
         * Log these data
         * 
         * @param title
         * @param details
         * @returns
         * 
         * Created on 22-Feb-2019 by Maria
         */
        function logme(title, details) {
            log.debug({
                title: title,
                details: details
            });
        }
    
});

Leave a comment

Your email address will not be published. Required fields are marked *