Saving Search Result contents in file

Scenario:

Adding search details to a new text file. Search details are converting to string and saving that file.

/**
         * Function to create the search of Employees who is a Project resource.
         * @returns [{res}] res - searchArray of object
         */
        function prjManagerSearch(){
            try{
                var employeeSearchObj = search.create({
                    type: "employee",
                    filters:
                        [
                            ["isinactive","is","F"],
                            "AND",
                            ["isjobresource","is","T"]
                        ],
                    columns:
                        [
                            search.createColumn({name: "internalid", label: "Internal ID"}),
                            search.createColumn({
                                name: "entityid",
                                sort: search.Sort.ASC,
                                label: "Name"
                            }),
                            search.createColumn({name: "email", label: "Email"})
                        ]
                });
                var searchResultCount = employeeSearchObj.runPaged().count;
                var res = []
                employeeSearchObj.run().each(function(result){
                    // .run().each has a limit of 4,000 results
                    var emp = result.getValue({name: "internalid"})
                    var empTxt = result.getValue({name: "entityid"})
                    var empMail = result.getValue({name: "email"})
                    res.push({
                        Employee: emp,
                        EmployeeTxt: empTxt,
                        EmployeeMail: empMail
                    })
                    return true;
                });
                return res;
            }
            catch (e) {
                log.error({
                    title: 'Error @ Prj Manager Search: ',
                    details: e.name + ' : ' + e.message
                });
            }
        }

function getData(){
try{
     var newFileName = 'FILE CONTENTS ['+index+' - '+limit+']'+ new Date()+'.txt'
var newFile = file.create({
       name: newFileName,
       folder: 15589,// create a new folder and replace the newly added folder's internal ID here
       fileType: file.Type.PLAINTEXT
     })
const fileId = newFile.save()

var prjMngr = prjManagerSearch()
 for(var i=0;i<prjMngr.length;i++){
       var txtObj = JSON.stringify(timesheetRes))
       let fileObj = file.load({
            id: fileId
       })
       fileObj.appendLine({value: txtObj})
       fileObj.save()
 }
}
catch (e) {
                log.error({
                    title: 'Error @ Prj Manager Search: ',
                    details: e.name + ' : ' + e.message
                });
            }
}


Leave a comment

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