- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2016 03:19 PM
I've added a u_last_query_date field to the cmdb_rel_ci table. I need a rule that says when you query that table, and you explicitly add the parent, child, and type fields, then I want that record queried to set the u_last_query_date field to the current date. A business rule with Query is possible, but I don't seem to have access to the record actually being queried. Any ideas how I can accomplish this?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2016 03:55 PM
You can get to the query that is being run by accessing current.getEncodedQuery(). From that you could create a GlideRecord query of your own and try to get the individual records that are being returned. You should also be able to look at the encoded query and see if the fields you want to check are being explicitly searched for. The only thing that worries me here, is I don't know if the query business rule is triggered from a GlideRecord query. If it is, then you may get yourself into an infinite loop.
var encQuery = current.getEncodedQuery();
if(encQuery.indexOf('parent=') >= 0 && encQuery.indexOf('child=') >= 0 && encQuery.indexOf('type=') >= 0)
{
var grRel = new GlideRecord('cmdb_rel_ci');
grRel.addEncodedQuery(encQuery);
grRel.query()
while(grRel.next())
{
grRel.u_last_query_date = gs.now();
grRel.update();
}
}
-Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2016 03:41 PM
Dosnt matter. If you can create business rule. It will run on server side. So it should work. Let me know if my understanding is wrong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2016 03:55 PM
You can get to the query that is being run by accessing current.getEncodedQuery(). From that you could create a GlideRecord query of your own and try to get the individual records that are being returned. You should also be able to look at the encoded query and see if the fields you want to check are being explicitly searched for. The only thing that worries me here, is I don't know if the query business rule is triggered from a GlideRecord query. If it is, then you may get yourself into an infinite loop.
var encQuery = current.getEncodedQuery();
if(encQuery.indexOf('parent=') >= 0 && encQuery.indexOf('child=') >= 0 && encQuery.indexOf('type=') >= 0)
{
var grRel = new GlideRecord('cmdb_rel_ci');
grRel.addEncodedQuery(encQuery);
grRel.query()
while(grRel.next())
{
grRel.u_last_query_date = gs.now();
grRel.update();
}
}
-Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2016 04:16 PM
It's definitely triggered that way But I can probably setWorkflow(false) to prevent the recursion. I'll have to try that out and see in one of our test environments.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2016 04:22 PM
Worked like a charm. Thanks!