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-21-2023 05:23 PM - edited ‎03-21-2023 05:23 PM
Hi @moncys ,
Try putting some logs in the script include and see the value you are getting for capacitySum before returning true or false and checking if the value is calculated correctly.
Also, please double-check the backend name of the capacity field.
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2023 02:34 AM
Hi Sarthak,
Thank you so much for your response.
I missed to add one thing here. I am unable to see debug log messages(in debug log section of SN application) for script include code while running the script. I am only seeing the alert messages that I put in client script. I am not even getting the Info messages pop-up in the form.
I tried to add the below lines ,but didnt work.
- gs.addInfoMessage('capacity '+capacity );
- gs.log(capacity);
- gs.info(capacity);
- Tried Script-Background method
Please advise.
Thanks,
Moncy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2023 07:28 PM
I was intruiged what chatGPT would say if I dumped your entire question in. This is the response it gave.
*note, I'm not saying it's correct; I'm just working through its response now and thought I'd share it as is.
ChatGPT response:
Your approach to check the capacity of the assigned user before creating a new ticket seems correct. However, there are a few issues in your code snippet.
Firstly, in your getCapacity function, you are only checking the capacity of the first open ticket that matches the assigned_to and state conditions. You should loop through all the open tickets assigned to the user and calculate the sum of their capacities.
Secondly, in your onChange client script, you need to define the checkCapacity function to parse the response from the server. You can use the response.responseXML.documentElement.getAttribute("answer") method to get the response from the server.
Here's the updated code snippet for your reference:
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();
while (gr.next()) {
capacitySum += gr.u_capacity;
}
capacitySum += parseInt(capacity);
if (capacitySum > 100) {
return false;
} else {
return true;
}
},
type: 'SPOC_TicketAjaxUtils'
};
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
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) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer === 'false') {
alert("Capacity exceeded! Please assign the ticket to another user.");
// Reset the assigned_to field to the previous value
g_form.setValue('assigned_to', oldValue);
}
}
}
In the checkCapacity function, you can compare the value of answer with the string 'false' to check if the capacity limit has been exceeded. If so, you can display an alert message to the user and reset the assigned_to field to the previous value.
Note: Make sure that the SPOC_TicketAjaxUtils Script Include is set to Client callable in its properties, and the onChange function is set to run on the assigned_to field change.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2023 02:36 AM
Hi James,
Thank you so much for your response. I will try to run the updated code.
I missed to add one thing here. I am unable to see debug log messages(in debug log section of SN application) for script include code while running the script. I am only seeing the alert messages that I put in client script. I am not even getting the Info messages pop-up in the form.
I tried to add the below lines ,but didnt work.
- gs.addInfoMessage('capacity '+capacity );
- gs.log(capacity);
- gs.info(capacity);
- Tried Script-Background method
Please advise.
Thanks,
Moncy