Find the list of scripts with inactive script owners using Saved Search

Scenario

The user will be cleaning up their account and will be reassigning ownership of the scripts with inactive script owners. To achieve this, they must first know which scripts are owned by inactive employees.

Solution

This can be achieved by running two saved searches in a script. One is to identify which employees are tagged as inactive, and another is to identify the script records they own. The results will then be exported in a CSV File and stored in the File Cabinet.

You may use the sample script below as a reference.

/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 * @NModuleScope SameAccount
 */

define(["N/search","N/file"],
function(search, file) {


  /**
   * Definition of the Scheduled script trigger point.
   *
   * @param {Object} scriptContext
   * @param {string} scriptContext.type - The context in which the script is executed. It is one of the values from the scriptContext.InvocationType enum.
   * @Since 2015.2
   */

  function execute(scriptContext) {
   var csvString = '';
   var counter = 0;
   var searchEmployees = search.load({
   id: 133
   });

   var empResultSet = searchEmployees.run();
   empResultSet.each(function(result){
   var empInternalId = result.getValue({
   name: 'internalid'
   });

  
    var ownerSearch = search.create({
    type: "script",
    filters: [   
     ["owner","anyof",empInternalId]
    ], columns:
    [
     search.createColumn({
       name: "name",
       sort: search.Sort.ASC,
       label: "Name"
     }),
     search.createColumn({name: "scriptid", label: "Script ID"}),
     search.createColumn({name: "scripttype", label: "Script Type"}),
     search.createColumn({name: "owner", label: "Owner"}),
     search.createColumn({name: "isinactive", label: "Inactive"})
    ]

  });

 

    if(counter == 0){
    for(var i = 0; i < ownerSearch.columns.length;i++){
    log.debug(ownerSearch.columns[i].name);
    if(i != ownerSearch.columns.length-1){
      csvString += ownerSearch.columns[i].label + ","
    } else {
      csvString += ownerSearch.columns[i].label + '\n';
    }
    }
    counter++;
    }

ownerSearch.run().each(function(ownerResult){
for(var i=0; i < ownerSearch.columns.length;i++){
log.debug(ownerSearch.columns[i].name);
var searchResult = ownerResult.getText({
name: ownerSearch.columns[i].name
});

log.debug("Search Result",searchResult)
if (searchResult == null){
var searchResult = ownerResult.getValue({
name: ownerSearch.columns[i].name
});

}
 
if(i != ownerSearch.columns.length-1){
   csvString += searchResult + ',';
   } else {
   csvString += searchResult + '\n';
   }
}

log.debug(csvString);
});

return true;
});

var date = new Date().toJSON();
var csvFile = file.create({
name: 'search_invalid_owner'+date+'.csv',
fileType: file.Type.CSV,
contents: csvString,
folder: '1255' // Folder ID where the file should be saved in the File Cabinet
});
var csvFileID = csvFile.save();
}
  return {
    execute: execute
  };
});

Leave a comment

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