/**
* Fetches active employee data with specific custom entity fields.
*
* This function creates and runs a search to find employees who are active (not inactive),
* have a specific custom entity field (custentity33) set to true, and have another custom entity field (custentity34) not empty.
* The results include the employee's name, region, and internal ID.
*
* @returns {Array<Object>} An array of objects representing the employee data, with each object containing:
* - name {string}: The name of the employee.
* - territory {string}: The region associated with the employee.
* - internalId {string}: The internal ID of the employee.
*
* @throws {Error} Throws an error if there is an issue executing the search or processing the results.
*/
const fetchEmployeeData = () => {
try {
let employeeSearchObj = search.create({
type: 'employee',
filters: [
['isinactive', 'is', 'F'],
'AND',
["custentity33", "is", "T"],
"AND",
["custentity34", "noneof", "@NONE@"],
],
columns: [
search.createColumn({
name: 'entityid',
summary: 'GROUP',
label: 'Name'
}),
search.createColumn({
name: "custentity34",
summary: "GROUP",
label: "Region"
}),
search.createColumn({
name: 'internalid',
summary: 'GROUP',
label: 'Internal ID'
}),
]
});
let searchResults = [];
employeeSearchObj.run().each(result => {
const managerId = result.getValue({ name: 'internalid', summary: 'GROUP' });
const subordinates = fetchSubordinates(managerId);
searchResults.push({
name: result.getValue({ name: 'entityid', summary: 'GROUP' }),
territory: result.getText({ name: 'custentity34', summary: 'GROUP' }),
internalId: result.getValue({ name: 'internalid', summary: 'GROUP' }),
subordinates: subordinates
});
return true; // continue processing
});
return searchResults;
} catch (error) {
log.error('Error fetching employee data', error);
throw error;
}
};
/**
* Fetches the subordinates of a given sales manager who are active sales representatives.
*
* This function performs a search for employee records where the given sales manager is the supervisor,
* the employee is a sales representative, and the employee is not inactive. It returns a list of subordinates
* with their names and internal IDs.
*
* @param {number|string} salesManagerId - The internal ID of the sales manager.
* @returns {Array<Object>} An array of objects, each containing the name and internal ID of a subordinate.
* @throws {Error} If there is an issue with the search or data retrieval, an error is logged and thrown.
*/
const fetchSubordinates = (salesManagerId) => {
try {
let subordinateSearchObj = search.create({
type: 'employee',
filters: [
["supervisor", "anyof", salesManagerId],
"AND",
["salesrep", "is", "T"],
],
columns: [
search.createColumn({ name: 'entityid', label: 'Name' }),
search.createColumn({ name: 'internalid', label: 'Internal ID' }),
search.createColumn({ name: 'custentity33', label: 'Is Sales Manager' }),
search.createColumn({ name: 'custentity35', label: 'Include In Leaderboard' }),
]
});
let searchResults = [];
subordinateSearchObj.run().each(result => {
let internalId = result.getValue({ name: 'internalid' });
let includeInLeaderboard = result.getValue({ name: 'custentity35' });
if (includeInLeaderboard)
searchResults.push({
name: result.getValue({ name: 'entityid' }),
internalId: internalId
});
searchResults.push(...fetchSubordinates(internalId))
return true;
});
return searchResults;
} catch (error) {
log.error('Error fetching subordinates', error);
throw error;
}
};