RE: Scheduled script should be assign cases until case count reaches 5.

Hi,
I have created a script that assigns the cases to all assignees first who have less then 5 cases of each until individuals case count reaches 5. then assigns rest cases equally to all.
But ti assigns only single time to  who have less then 5 cases while it should assign until case count reaches 5. Your guidance is highly appreciated

[

/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/
define(['N/search', 'N/record', 'N/log', 'N/currentRecord', 'N/error'],
function (search, record, log, currentRecord, error) {

function assignCasesToAssignees() {
//Search Employyee
var assigneeCaseCount = {};
var myAssignee = [];
var caseCount = [];
var emplSearch = search.create({
type: search.Type.EMPLOYEE,
columns: [
search.createColumn({
name: 'internalid',
sort: search.Sort.ASC
})
],
filters: [["isinactive","is","F"],"AND",
["internalid","anyof",'121212', '131313', '141414', '151515', '161616'], "AND",
["supportrep","is","T"]]
});
var empSearchResult = emplSearch.run().getRange({
start: 0,
end: 1000
});
for (var i = 0; i < empSearchResult.length; i++) {
myAssignee = empSearchResult[i].id;

// Find all cases excluding closed
var caseSearch = search.create({
type: search.Type.SUPPORT_CASE,
filters: [
["isinactive","is","F"],
"AND",
["custevent_case_department","anyof","1","12"],
"AND",
["status","noneof","5"],
"AND",
["employee.custentity27","anyof","1","12"],
"AND",
["assigned","anyof",myAssignee]
],
columns: [
search.createColumn({
name: 'internalid',
summary: search.Summary.COUNT,
sort: search.Sort.ASC
})
]
});

var result = caseSearch.run().getRange({
start: 0,
end: 1
});
caseCount = result.length > 0 ? result[0].getValue({
name: 'internalid',
summary: search.Summary.COUNT,
sort: search.Sort.ASC
}) : 0;
// Create an object to store the number of cases for each assignee
assigneeCaseCount[myAssignee] = caseCount;
}
// Convert object to array of objects and sort based on case count
var assigneeArray = [];
for (var assignee in assigneeCaseCount) {
assigneeArray.push({assignee: assignee, caseCount: assigneeCaseCount[assignee]});
}
assigneeArray.sort(function(a, b) {
return a.caseCount - b.caseCount;
});

log.debug({title: 'assigneeCaseCount', details: assigneeArray});

// Find all assignees with fewer than 5 cases
var eligibleAssignees = [];

for (var i = 0; i < assigneeArray.length; i++) {
if (assigneeArray[i].caseCount < 5) {
eligibleAssignees.push(assigneeArray[i].assignee);
}
}

log.debug({title: 'eligibleAssignees', details: eligibleAssignees})
// Assign cases to eligible assignees
var caseSearch2 = search.create({
type: search.Type.SUPPORT_CASE,
filters: [
search.createFilter({
name: 'status',
operator: search.Operator.IS,
values: [1]
}),
search.createFilter({
name: 'custevent_case_department',
operator: search.Operator.IS,
values: [1]
}),
search.createFilter({
name: 'assigned',
operator: search.Operator.ANYOF,
values: '@NONE@'
}),
search.createFilter({
name: 'isinactive',
operator: search.Operator.IS,
values: ['F']
})
/*search.createFilter({
name: 'internalid',
operator: search.Operator.IS,
values: 29752252
}),
search.createFilter({
name: 'title',
operator: search.Operator.DOESNOTCONTAIN,
values: ['Appointment']
}),// excluding appointment cases.
search.createFilter({
name: 'title',
operator: search.Operator.DOESNOTCONTAIN,
values: ['alsoenergy01/1-971-801-6025']
}),// excluding Voice mails cases.
// cases not assigned to anyone.
search.createFilter({
name: 'title',
operator: search.Operator.DOESNOTCONTAIN,
values: 'RE: Case # 414545 Created: RE: Case # 414544 Created: RE: Case # 414538 Created: RE: Case # 414535 Created: RE: Case # 414533 Created: RE: Case # 414531 Created: RE: Case # 414526 Created: RE: Case #'
})*/
],
columns: [
search.createColumn({
name: 'internalid'
})
]
});
var caseIds = [];
caseSearch2.run().each(function(result) {
caseIds.push(result.getValue({ name: 'internalid' }));
return true;
});
for (var i = 0; i < caseIds.length; i++) {
var caseId = caseIds[i];
log.debug({
title : 'caseId',
details: caseId
})
if (eligibleAssignees.length > 0) {
var testassignee = eligibleAssignees.shift();
log.debug({title: 'assignee after shifting', details: testassignee});
var saverecord = record.submitFields({
type: record.Type.SUPPORT_CASE,
id: caseId, //414439
values: {
'assigned': testassignee,
'status': 1 // 1 = Not-started, 2 = In-progress
}
});
log.debug({title: 'Assigned who has <5 cases', details: 'Assignee - '+testassignee+', Case ID -'+saverecord});
assigneeCaseCount[assignee]++;
if (assigneeCaseCount[assignee] >= 5) {
// Remove assignee from the eligible assignees list
var index = eligibleAssignees.indexOf(assignee);
if (index !== -1) {
eligibleAssignees.splice(index, 1);
}
}
} else {
// No more eligible assignees, assign cases equally to all assignees
var assignees = Object.keys(assigneeCaseCount);
var assigneeIndex = i % assignees.length;
var assignee = assignees[assigneeIndex];
//log.debug({title: 'assignees[assigneeIndex]', details: assignee})
var equallyAssignement = record.submitFields({
type: record.Type.SUPPORT_CASE,
id: caseId, //414439
values: {
'assigned': assignee,
'status': 1 // 1 = Not-started, 2 = In-progress
},
options: {
enableSourcing: false,
ignoreMandatoryFields : true
}
});
log.debug({title: 'Aqually Assignement >5', details: 'Assignee - '+assignee+', Case ID -'+equallyAssignement});
assigneeCaseCount[assignee]++;
}
}
}

return{
execute:assignCasesToAssignees
}

})

].

 

Titu Beginner Asked on March 29, 2023 in SuiteScript.
Add Comment
1 Answers

Hi,

I think the issue is wit this line:

var testassignee = eligibleAssignees.shift();

.shift() removed the element from the array and so I think you’re OK with:

var testassignee = eligibleAssignees[0];

since you’re removing them when they’re full.

The only thing with this is that it will fill up Assignee 1 before moving on to Assignee 2, etc. I’m wondering whether you want to give everyone an extra case before looping round again to help distribute the workload?

Thanks,

Chris

Intermediate Answered on March 29, 2023.

Thanks @Chris for your comment! Actually, I want that it should be assign the cases to  assignee 1 until his case count reaches 5 before moving on to next assignee. for exp: there are 3 assignees A, B, C with current case count 3, 4, 5.  And 6 cases more which need to be assign to all of three but It should be assign 2 cases to A and 1 case to B. Then it should be assign rest 3 cases to all equally. While it is assigning 1 case to A and 1 to B then assigning equally that’s why case count reaches 5, 6, 7 while it should be 6, 6, 6 of each.

on March 30, 2023.

Hi,

I was thinking about the example where they have 1, 1, 1 and you got 4 new cases.

This will result in 5, 1, 1 which seems a bit inefficient.

Thanks,

Chris

on March 30, 2023.

Hi,

In this case result should be 3, 2, 2.
My requirement is very simple that if 3 out of 5 assignees have less then 5 cases then cases should be assign one by one to 3 assignees until their case count reaches 5 after that remaining cases should be assign to all 5 assignees one by one.

I want to distribute the workload to all equally.

on March 30, 2023.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.