Reputation: 45
I am creating one report. In report condition I am using 'is one of' and passing sys_id array by calling client callable script include javascript: new GetMoreInfoUpdatedChangeTickets().getSysIds();as. I can see sys_ids as "Data source conditions:Sys ID in 60a219744fc73200d6940e428110c72b". But corresponding record is not getting displayed. But corresponding sys_id record is available.
Upvotes: 2
Views: 1904
Reputation: 601
I think I see the issue here. Unfortunately, that is not how client-callable script includes typically work. Script includes, even client-callable ones, ALWAYS execute on the server. They are meant to be called via an asynchronous GlideAjax call from a client-side script.
For more information on this, I encourage you to have a look at this article on GlideAjax.
However, luckily for you, your actual query executes on the server, not the client. So a Script Include is probably the right way to go, but the problem you're facing is (probably) that by making it a GlideAjax script include, you're telling ServiceNow to override the constructor (initialize()) method, but leave it as a Class.
Since you say you're getting a sys_id returned, it's possible that what I described isn't the issue, and this solution won't work. However, I don't have enough information to give any other answer; so if that's the case, please run the query that isn't showing you the correct results, then right-click on the last query breadcrumb at the top of the list, and click "Copy Query", then paste the query in a reply on this thread (replying specifically to my comment so I get notified) so I can have a look.
Please also obviously provide the code in your script include so I can help you troubleshoot.
EDIT: Now what you've added your code in a reply to this comment, I can see several issues.
Your original:
var GetMoreInfoUpdatedChangeTickets = Class.create();
GetMoreInfoUpdatedChangeTickets.prototype = {
initialize: function() {
}, getSysIds: function getMyGroupMembers() {
var ga = new GlideRecord('sysapproval_group');
ga.addQuery('parent.sys_class_name', '=', 'change_request');
ga.query();
gs.log("TotalRecords1 Before:: " + ga.getRowCount());
var sysIdArray = [];
while (ga.next()) {
sysIdArray.push(ga.sys_id);
}
return sysIdArray;
}, type: 'GetMoreInfoUpdatedChangeTickets'
}
Here are all of the changes I made to your code, and an explanation for each:
First, on line 5 in the code below, I've changed how your member function (aka: "method") is declared. You had it declared like so:
getSysIds: function getMyGroupMembers() {}
So here, you've essentially given it two names and declared it in two ways at once.
Normally, a function is declared like this:
function functionName(args) {}
However, inside of an object (or in this case, a Class - which is an object with a constructor that returns copies of itself using the "new" keyword) we declare functions like this:
functionName: function(args) {},
So as you can see in the code below, I've made that adjustment for you.
Next, on line 8 below, I've removed the redundant '=' operator from your addQuery()
method call, as "=" is implied when not specified. This isn't required, but it looks cleaner this way.
(I've also renamed your GlideRecord variable from ga
to grApprovalGroup
throughout, so that it makes more sense.)
I've moved the declaration of sysIdArray
to the top of the function for consistency with how it's executed (and for clarity). This is also not required, but is generally recommended.
Line 12 below, in your original code, looked like this:
sysIdArray.push(grApprovalGroup.sys_id);
However, since gr.sys_id
is an object (type: "GlideElement" - documentation here), this is pushing a reference to that object into the array. As the string value property of that object (the one at location gr.sys_id
changes, so does the string value property of each and every element in the array, since every single element is a reference to the same object! More information on this, in an article (which I wrote) that you can find here.
Suffice it to say that this is not a good idea, and will result in a list of the same sys_id over and over! To fix this, we simply use the best-practice recommended "getter" method (as the article I linked recommends), as you can see on line 12 in the code below.
var GetMoreInfoUpdatedChangeTickets = Class.create();
GetMoreInfoUpdatedChangeTickets.prototype = {
initialize: function() {
},
getSysIds: function() {
var sysIdArray = [];
var grApprovalGroup = new GlideRecord('sysapproval_group');
grApprovalGroup.addQuery('parent.sys_class_name', 'change_request');
grApprovalGroup.query();
while (grApprovalGroup.next()) {
sysIdArray.push(grApprovalGroup.getValue('sys_id'));
}
return sysIdArray;
},
type: 'GetMoreInfoUpdatedChangeTickets'
};
Finally, we need to actually extract the function from the class and rename it so the script include itself is just the function.
function GetMoreInfoUpdatedChangeTickets() {
var sysIdArray = [];
var grApprovalGroup = new GlideRecord('sysapproval_group');
grApprovalGroup.addQuery('parent.sys_class_name', 'change_request');
grApprovalGroup.query();
while (grApprovalGroup.next()) {
sysIdArray.push(grApprovalGroup.getValue('sys_id'));
}
return sysIdArray;
}
Since you can't use the "new" keyword in query javascript, it's necessary to define it like this (without the class), and once we've done that we can re-enable the "client callable" option you had enabled previously.
Once you've done that, you should be able to go to the following URL, and see your query script in action:
Upvotes: 3