Hi Team, Please help me how to get solution for below Question ....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2024 05:52 AM - edited ‎03-03-2024 05:54 AM
Create a custom table with 2 fields(username(reference type), waiting for approval request count(string type)). Write a script include when any of user is selected on username field then fetch the count of all approvals are available for current selected user.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2024 06:35 AM
Hi,
Isn't it too much to create a table itself for this requirement rather than creating a simple report that can do it (pivot type)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2024 06:41 AM
Hi @rrrrrrrrrrrr,
Here is a solution for your problem.
1. **Create a Custom Table:**
- Navigate to `System Definition` > `Tables`.
- Click on `New` to create a new table.
- Define your table with the required fields (`username` as a reference type and `waiting_for_approval_request_count` as a string type).
- Save the table.
2. **Write a Script Include:**
- Navigate to `System Definition` > `Script Includes`.
- Click on `New` to create a new script include.
- Define your script include with a suitable name (e.g., `ApprovalRequestCountUtil`).
- Write a function in the script include to fetch the count of approval requests for a given user. Here's an example function:
Script
var ApprovalRequestCountUtil = Class.create();
ApprovalRequestCountUtil.prototype = {
initialize: function() {},
// Function to get the count of approval requests for a user
getApprovalRequestCount: function(userId) {
var count = 0;
var approvalGr = new GlideRecord('sysapproval_approver');
approvalGr.addQuery('approver', userId);
approvalGr.addQuery('state', 'requested');
approvalGr.query();
while (approvalGr.next()) {
count++;
}
return count.toString();
},
type: 'ApprovalRequestCountUtil'
};
```
- Save the script include.
3. **Usage in Client Script:**
- Now, you need to use this script include in a client script attached to the form where the `username` field is present.
- In the client script, write code to call the `getApprovalRequestCount` function from the script include when a user is selected in the `username` field. You can then update the `waiting_for_approval_request_count` field with the count returned by the function.
Here's an example of how you might write the client script:
Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var approvalCountUtil = new ApprovalRequestCountUtil();
var count = approvalCountUtil.getApprovalRequestCount(newValue);
g_form.setValue('waiting_for_approval_request_count', count);
}
```
Replace `'waiting_for_approval_request_count'` with the actual field name where you want to display the count of approval requests.
With this setup, when a user is selected in the `username` field, the `waiting_for_approval_request_count` field will be updated with the count of approval requests for that user, fetched using the script include. Adjust the code as needed based on your specific field names and requirements.
Please hit helpful and accept my answer as a solution if it solved your problem.
Regards,
M.Ismail
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2024 07:22 AM
You want to see the count attached to that user for approval. Not sure what is need to big script , you can do via report.
Like this
I create report on sys approval table, you can use your table
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************