Update GlideRecord on query

grosch
Mega Expert

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?

1 ACCEPTED SOLUTION

Steve McCarty
Mega Guru

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


View solution in original post

4 REPLIES 4

balsu333
Mega Expert

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


Steve McCarty
Mega Guru

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


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.


Worked like a charm.   Thanks!