Run a business rule from a background script

mdjoseph12
Giga Contributor

Is there a way that i can run a business rule from a background script to modify records that were skipped on insert: 

 

(function executeRule(current, previous /*null when async*/) {
	var manufacturer=current.company.getValue('sys_id');
	var warranty;
	var warranty2;
	var warrantydate;
	var gr = new GlideRecord("u_manufacturer_warranty");
	gr.addQuery('u_manufacturer',manufacturer);
	//gr.addQuery('u_model',manufacturer);
	gr.query();
	
	while(gr.next()) {
		warranty = gr.u_warranty_period.dateNumericValue();
	    warranty2 = (warranty/1000);
		warrantydate = new GlideDateTime(current.install_date.getDisplayValue());
		warrantydate.addSeconds(warranty2);
		
	}
	current.warranty_expiration = warrantydate;
	
})(current, previous);
1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Take the core of your script and loop around it. You can even use 'current' as the record name. Something like this:

var current = new GlideRecord("YOURTABLEHERE");
// Add queries to current
current.query();

while (current.next()) {
	var manufacturer=current.company.getValue('sys_id');
	var warranty;
	var warranty2;
	var warrantydate;
	var gr = new GlideRecord("u_manufacturer_warranty");
	gr.addQuery('u_manufacturer',manufacturer);
	//gr.addQuery('u_model',manufacturer);
	gr.query();
	
	while(gr.next()) {
		warranty = gr.u_warranty_period.dateNumericValue();
	    warranty2 = (warranty/1000);
		warrantydate = new GlideDateTime(current.install_date.getDisplayValue());
		warrantydate.addSeconds(warranty2);
		
	}
	current.warranty_expiration = warrantydate;
        current.update(); // DON'T FORGET TO ADD THIS!
}

View solution in original post

3 REPLIES 3

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

 

If you can loop through records that were skipped on insert, in a background script, then you should be able to run the same script for these records in the background.

Copy the same script and instead of current just use the glide object of those records.

 

-Anurag

-Anurag

Trupti6
Tera Expert

Hi,

Instaed of using current object you can put sys id in script as below in background script

 

(function executeRule(current, previous /*null when async*/)

{

var manufacturer='sys_id'; // var warranty;

var warranty2; var warrantydate;

 })(current, previous);

 

Thanks,

Trupti S.

Chuck Tomasi
Tera Patron

Take the core of your script and loop around it. You can even use 'current' as the record name. Something like this:

var current = new GlideRecord("YOURTABLEHERE");
// Add queries to current
current.query();

while (current.next()) {
	var manufacturer=current.company.getValue('sys_id');
	var warranty;
	var warranty2;
	var warrantydate;
	var gr = new GlideRecord("u_manufacturer_warranty");
	gr.addQuery('u_manufacturer',manufacturer);
	//gr.addQuery('u_model',manufacturer);
	gr.query();
	
	while(gr.next()) {
		warranty = gr.u_warranty_period.dateNumericValue();
	    warranty2 = (warranty/1000);
		warrantydate = new GlideDateTime(current.install_date.getDisplayValue());
		warrantydate.addSeconds(warranty2);
		
	}
	current.warranty_expiration = warrantydate;
        current.update(); // DON'T FORGET TO ADD THIS!
}