Finding the total capacity of tickets assigned to a user
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2023 02:54 PM
Hi,
I am new to ServiceNow platform. I have got a requirement in which customer has custom table [ticket] for storing Tickets. This table extends Task table. One of the custom fields is Capacity [u_capacity], type of Integer. Each person in the
system can be assigned to tickets only if sum of u_capacity from open tickets assigned to him/her is
no more than 100. Let’s assume that capacity on tickets are never updated after insertion.
Below is my approach to achieve this functionality. Kindly let me know if this approach is correct or not and also I am stuck in the client script while parsing the response from the server.
I have a client callable Script Include which contains function that takes two arguments:
User sys_id of assigned_to, needed_capacity is a value of capacity that is required for a
new ticket you want to create.It returns false if summed up capacity of all open tickets
assigned to this user and required capacity is greater than 100 and true if not.
Please find my script include code snippet below:
var SPOC_TicketAjaxUtils = Class.create();
SPOC_TicketAjaxUtils.prototype = {
initialize: function() {},
getCapacity: function() {
var userID = this.getParameter('sysparm_user');
var capacity = this.getParameter('sysparm_needed_capacity');
var capacitySum = 0;
var gr = new GlideRecord('x_903407_ticketing_x_903407_ticket');
gr.addQuery('assigned_to', userID);
gr.addQuery('state', '1');
gr.query();
if (gr.next()) {
capacitySum = capacitySum + gr.capacity;
}
capacitySum = capacitySum + capacity; //adding with the new capcity entry
if(capacitySum > 100){
return false;
}
else{
return true;
}
},
type: 'SPOC_TicketAjaxUtils'
};
Note: 'state' is the status of the ticket. We will consider only Open tickets.
My client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('SPOC_TicketAjaxUtils');
ga.addParam('sysparm_name', 'getCapacity');
ga.addParam('sysparm_user', g_user.userID);
ga.addParam('sysparm_needed_capacity', newValue);
ga.getXML(checkCapacity);
function checkCapacity(response) {
alert(response);
// var answer = response.responseXML.documentElement.getAttribute("answer");
// var result = JSON.parse(answer);
}
}
The response is coming as null while running the script. Please let me know if this is the way to parse the response in client script.
Thanks,
Moncy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2023 07:01 PM - edited ‎03-23-2023 07:01 PM
Hi @moncys
You can disregard the chatGPT dump I did earlier. It was trying to update what you had set about to do in the code you provided.
If I were in your shoes, I'd probably approach things differently. There is no need to use the 'u_capactiy' field (you could, but it's unnecessary).
Write a Business Rule, and set it on before 'insert' or 'update' to do the logic. The filter condition can simply be if 'assigned_to' is not empty. Use the following script in the advanced script section:
(function executeRule(current, previous /*null when async*/) {
// Query for all open tickets assigned to the assigned_to user
var ticketGR = new GlideRecord('ticket');
ticketGR.addQuery('active=true^assigned_to=' + current.assigned_to);
ticketGR.query();
var ticketCount = 0;
// Loop through each open ticket assigned to the assigned_to user and count them
while (ticketGR.next()) {
ticketCount++;
}
// Check if the ticket count exceeds 100
if (ticketCount >= 100) {
gs.addErrorMessage("The assigned user already has 100 open tickets assigned to them.");
current.setAbortAction(true);
}
})(current, previous);
I tested this on the Incident table, temporarily changing the number to 10 for testing. It works as expected; users cannot assign a ticket to a person if that person has 'X' amount of tickets already assigned to them.
Regards
James
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2023 08:03 PM
@moncys How did you go, is everything sorted?
If you found my answer above correct, mark it as such to help others find it.
Regards
James