Running a Business Rule from an Scheduled job

jtorres
Kilo Contributor

Hello everyone.

I have a Business Rule, and I want to run it periodically, to validate an expiration date on a record, the BR works properly, but because of the nature of BRs I have to either update or insert the form, for it to trigger, I read that we could run it from an scheduled job, but I haven't found the way to set the relation between them.

var now = gs.endOfYesterday();

var vigencia = current.u_fecha_fin; //expiration dateif(vigencia < now){

current.u_hrs_vencidas = current.u_hrs_restantes;

current.u_hrs_restantes = 0;

current.u_activo = 'true';

gs.addInfoMessage("TEST");

}

Could anyone please help me find the best way to do this?

Thanks in advance and regards!.

1 ACCEPTED SOLUTION

crhistopherjuar
Kilo Expert

Hi Irving,


I recommend to you, this app developed by Chui Tomasi.



ServiceNow Share


Scriptless Scheduled Jobs - YouTube



P.S. Please hit like, helpful or correct for my responses as they apply to you


View solution in original post

15 REPLIES 15

veena_kvkk88
Mega Guru

Hi Irving,



You could run the same script in a scheduled Job but it doesn't necessarily mean that you have to call/refer to the Business rule. Why don't you try using the same script you have in the BR in the scheduled job? That way, same validation occurs while inserting or updating the record, as well as periodically.



Only difference is, instead of using current object to refer to the record, you will querying all records to walk through each one.


So then the idea is that we need to have a query to the table something like this?



var nies = gs.GlideRecord('u_periodos');


nies.query();


var now = gs.endOfYesterday();


var vigencia = current.u_fecha_fin; //expiration dateif(vigencia < now){


current.u_hrs_vencidas = current.u_hrs_restantes;


current.u_hrs_restantes = 0;


current.u_activo = 'true';


gs.addInfoMessage("TEST");


}



I'd really appreciate If you see something wrong with it and help me correct it, or additionally if you could give me some more detail or examples on how to create the scheduled job.



Thanks and regards!


Hi,



The Autoclose Incidents scheduled job calls the autoclose incidents Business rule. So have a look at that as an example. The BR does not have any Insert och Update check box selected, so it is only called by the scheduled job.



- Jan


Harish KM
Kilo Patron
Kilo Patron

You can see an example of this in the 'Autoclose Incidents' scheduled job. Navigate to 'System Scheduler -> Scheduled Jobs' to see it. The key is calling the business rule ('incident autoclose' in this case) by name in the 'Job context' field like this...



fcScriptName=incident autoclose// incident autoclose is BR name


find_real_file.png


Regards
Harish