Filters using suitelet script

Problem:

Create a suitelet script that loads a custom search US CASES – HOURS TO CLOSE search result in a given format for klipfolio
and create filters for ‘created date condition’ also replace the same condition with that of UI by using splice() method.

Solution

  1. load the custom search
  2. 2.Add the filters
  3. 3.Replace the condition from UI using splice() method
  4. 4.Stringify the result

.

/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
/*******************************************************************
 *   Suitelet Script returns search result for klipfolio
 ****************************************************************
 *
 * Date: 17/02/2022
 *
 * Author:  Jobin and Jismi IT Services LLP
 *
 * REVISION HISTORY
 *
 * Revision
 *
 * Description: Suitelet returns the KLIPFOLIO  - US CASES - HOURS TO CLOSE search result in a given format for klipfolio

 *
 ***************************************************************/
define(['N/search','N/format'],
    /**
 * @param{search} search
 */
    (search,format) => {
        /**
         *  @param function to Create search result in a given format
         *  @param returns search result
         */
        const onRequest = (scriptContext) => {
            try {
                var response = scriptContext.response;
                var  date =new Date();
                //converting current date to US time zone
                var usTime = format.format({
                    value : date,
                    type : format.Type.DATETIME,
                    timezone : format.Timezone.AMERICA_NEW_YORK
                });
                //setting the date as date before 7 days(week)
                let us_date=new Date(usTime.split(' ')[0]);
                let us_date1=new Date(usTime.split(' ')[0]);
                us_date.setDate(us_date.getDate() - 7);
                //setting from and to date
                var from = (us_date.getMonth()+1)+'/'+ us_date.getDate() +'/'+us_date.getFullYear();
                us_date1.setDate(us_date1.getDate());
                var to = (us_date1.getMonth()+1)+'/'+ us_date1.getDate() +'/'+us_date1.getFullYear();
                //load the search with id to obtain the required result
                var searcResult=search.load({
                    id:'customsearch5036'
                });
                //create filters for search filters:"createddate","onorbefore"," last week to date"(from..to)
                var filters = searcResult.filters;
                var filterOne = search.createFilter({
                    name: 'createddate',
                    operator: search.Operator.WITHIN,
                    values: [from , to]
                });
                //replace filter created date in the UI with that of script
                filters.splice(0,1,filterOne)
                log.debug('searcResult',searcResult);
                //Run the search result
                var results = searcResult.run().getRange(0,1000);
                //stringify the result and write the the result
                var newResult= response.writeLine(JSON.stringify(results));

            }catch (e) {
                log.error('on_request', e.message);
                log.debug('on_request', e.message);
            }

        }
        return {onRequest}
    });

Leave a comment

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