Saved Search to get the time bills in ascending order of the date

this code sample will give the time bills search result in the ascending order of the date

function timeBillSearchData(subsidiaryDateArr, monthYearComboArr, payrollArray) {

            try {

                let subsidiaryArray = subsidiaryDateArr.map(item => item.subsidiary);

                let dateFilters = [];

                monthYearComboArr.forEach(function (item, index) {

                    let startDate = format.format({

                        value: new Date(item.year, item.month 1, 1),

                        type: format.Type.DATE

                    });

                    let endDate = format.format({

                        value: new Date(item.year, item.month, 0),

                        type: format.Type.DATE

                    });

                    if (index > 0) {

                        dateFilters.push(“OR”);

                    }

                    dateFilters.push([

                        [“date”, “within”, startDate, endDate],

                    ]);

                });

                let timeBillFilter = [

                    [“duration”, “greaterthan”, “0”],

                    “AND”,

                    [“approvalstatus”, “anyof”, “3”],

                    “AND”,

                    [“custcol_jj_employee_type_time”, “isnot”, “Consultant”],

                    “AND”,

                    [“custcol_jj_project_allocation”, “noneof”, “@NONE@”],

                    “AND”,

                    [“subsidiary”, “anyof”, subsidiaryArray],

                    ‘AND’,

                    [“custcol_jj_project_allocation.custrecord_jj_project_period”, “anyof”, payrollArray],

                    ‘AND’,

                    dateFilters

                ]

                let timeBillArray = [];

                let timebillSearchObj = search.create({

                    type: “timebill”,

                    filters: [

                        timeBillFilter,

                        // ‘AND’, // ADDED FOR TESTING

                        // [“internalid”, “anyof”, [“3496433”, “3496434”, “3496435”, “3496436”, “3496437”, “3496432”,”3496438″]],

                    ],

                    columns:

                        [

                            search.createColumn({ name: “employee”, label: “Employee” }),

                            search.createColumn({ name: “customer”, label: “Donor” }),

                            search.createColumn({ name: “hours”, label: “Duration” }),

                            search.createColumn({ name: “internalid”, label: “Internal ID” }),

                            search.createColumn({ name: “approvalstatus”, label: “Approval Status” }),

                            search.createColumn({ name: “custcol_jj_project_allocation”, label: “Project Allocation” }),

                            search.createColumn({

                                name: “formulatext”,

                                formula: “CONCAT({date},{custcol_jj_project_allocation})”,

                                label: “Formula (Text)”

                            }),

                            search.createColumn({ name: “subsidiary”, label: “Regional Office” }),

                            search.createColumn({

                                name: “custrecord_jj_project_period”,

                                join: “CUSTCOL_JJ_PROJECT_ALLOCATION”,

                                label: “Payroll Record”

                            }),

                            search.createColumn({ name: “date”, sort: search.Sort.ASC, label: “Date” }),

                            search.createColumn({ name: “workcalendar”, join: “employee”, label: “Work Calendar” })

                        ]

                });

                let myPagedData = timebillSearchObj.runPaged()

                if (myPagedData.count > 0) {

                    myPagedData.pageRanges.forEach(function (pageRange) {

                        let myPage = myPagedData.fetch({ index: pageRange.index });

                        myPage.data.forEach(function (result) {

                            let obj = {}

                            obj.employee = result.getValue({ name: “employee”, label: “Employee” });

                            obj.project = result.getValue({ name: “customer”, label: “Donor” });

                            obj.hours = result.getValue({ name: “hours”, label: “Duration” });

                            obj.internalId = result.getValue({ name: “internalid”, label: “Internal ID” });

                            obj.approvalStatus = result.getValue({ name: “approvalstatus”, label: “Approval Status” });

                            obj.projectallocationId = result.getValue({ name: “custcol_jj_project_allocation”, label: “Project Allocation” });

                            obj.uniqueId = result.getValue({

                                name: “formulatext”,

                                formula: “CONCAT({date},{custcol_jj_project_allocation})”,

                                label: “Formula (Text)”

                            });

                            obj.subsidiary = result.getValue({ name: “subsidiary”, label: “Regional Office” });

                            obj.payrollId = result.getValue({

                                name: “custrecord_jj_project_period”,

                                join: “CUSTCOL_JJ_PROJECT_ALLOCATION”,

                                label: “Payroll Record”

                            });

                            obj.date = result.getValue({ name: “date”, label: “Date” });

                            obj.workCalendar = result.getValue({ name: “workcalendar”, join: “employee”, label: “Work Calendar” });

                            timeBillArray.push(obj);

                            return true;

                        });

                    });

                }

                return timeBillArray;

            }

            catch (error) {

                log.error(“Error @ timeBillSearchData”, error);

                return [];

            }

        }

Leave a comment

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