How to write query to find count for a condition
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 06:35 AM
I have the below scenario:
User is entering server name in the catalog form field(reference :windows server table).
1.If it is one server name it should accept(count is 1 now)
2. if two server are selected, then for second it should check if parent has "citrix" in name, If yes count is 1 now. If no :citrix in name of parent, count becomes 2. field value in form clears off as error.
3. if two server are selected, then for second it should check if parent has "citrix" in name. User enters 3rd server name- count is now 2 field value in form clears off as error.
I have the below SI for case 1. It is not working for case 2 and 3. Please help
var count = 0;
var server = this.getParameter('sysparm_server');
var serverrel = new GlideRecord('cmdb_rel_ci');
serverrel.addEncodedQuery('parent.operational_status=1^parent.sys_class_name=cmdb_ci_service_discovered^child.sys_id=' + server);
serverrel.query();
while (serverrel.next()) {
count++;
}
gs.log("count"+count);
return JSON.stringify(count);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 07:02 AM - edited 07-24-2023 07:05 AM
Hi @Anna_Servicenow ,
As I understood, you want a code to validate the selection of servers in a form. The code checks if the count of selected servers is 1 or 2. If the count is 2, the code checks if the parent of the second server has "Citrix" in the name. If it does, the code allows the selection. Otherwise, the code shows an error. If the count is more than 2, the code shows an error.
Please configure the onChange client script. Here is the code:
function onChangeServerField(control, oldValue, newValue, isLoading) {
if (!newValue || isLoading) return;
var selectedServers = g_form.getValue('server_field_name').split(',');
var selectedServersCount = selectedServers.length;
if (selectedServersCount === 1 || selectedServersCount === 2) {
var parentServer = g_form.getReference('parent_field_name');
if (selectedServersCount === 2 && (!parentServer || parentServer.name.indexOf('citrix') === -1)) {
g_form.showFieldMsg('server_field_name', 'The second server selection is invalid. Parent does not have "citrix" in the name.', 'error');
} else {
g_form.hideFieldMsg('server_field_name', true);
}
} else {
g_form.showFieldMsg('server_field_name', 'You can only select up to 2 servers.', 'error');
}
}
Please elaborate your requirement in detail, if I misunderstood something:)
If this helped you in any way, please hit the like button/mark it helpful. Also, don't forget to accept it as a solution. So it will help others to get the correct solution.
thanks,
Prasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 07:11 AM
Not exactly this am looking for.
When user enters 2nd server name(apparently child server) it should check in the cmdb_rel_ci table, check for parent.
if there is 2 parent for 1 child and one of the parent is citrix, count is 1 and if not citrix count is 2

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 10:41 PM
Hi @Anna_Servicenow ,
Please elaborate on your requirement in detail along with a screenshot.
Thanks,
Prasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2023 12:40 AM - edited 07-25-2023 12:41 AM
Please refer below screen shot:
On form I enter the first child value shown, count is returned as 1 . Now if i enter second value , count should still remain 1 as parent has citrix in name. If the second parent was not ientered or a different one without parent as citrix is entered, the count would have been 2