Business Rule: Get difference between a field in the parent record and a field in the child record

Desmo
Mega Guru

Hello Community,

 

I am attempting to get the different between two dates. First field is from the parent record; second field is from the child record (current). This is the first part of the script.

 

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here

    var gr = new GlideRecord('ast_contract');
    gr.addQuery('sys_id', current.parent);//query parent record
    gr.query();
    if (gr.next())

    var diff;
    var checkdate =  gr.sys_created_on;//get created date from parent record
    var now = new GlideDateTime();//current datetime
    var sched = new GlideSchedule('6ed55e1d13390c101c67d3228144b0ef');//use this schedule
    diff = sched.duration(startDate, now);//difference between dates
    gs.addInfoMessage(diff.getDurationValue());//display difference
	
})(current, previous);

 

Additionally, looking for help on the second part of the business rule:

  1. If the difference is >=365 days, set due date as sys_created_on + 180 days [M-F only]
  2. If the difference is =<180 days, set due date as sys_created_on + 365 days [M-F only]

Thank you.

3 REPLIES 3

Samaksh Wani
Giga Sage

Hello @Desmo 

 

 

diff=diff.getDurationValue();

if(diff>=365){
dueDate.addDays(checkdate + 180);
}
if(diff=<180){
dueDate.addDays(checkdate + 365);
}

 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Regards,

Samaksh

Bert_c1
Kilo Patron

Hi @Desmo 

 

I tried the logic in your BR script in scripts background, had to assume a schedule from the cmn_schedule table to test, fix the "if" statement for "gr.next())".  And use API methods that are documented. My test logic follows:

 

 

 

var gr = new GlideRecord('ast_contract');
// gr.addQuery('sys_id', current.parent);//query parent record
// Find a record older that a year - for testing
gr.addEncodedQuery('sys_created_on<=javascript&colon;gs.beginningOfLast12Months()');
gr.query();
while (gr.next()) {
	gs.info("brScript: Processing Contract: " + gr.vendor_contract + ", created: " + gr.sys_created_on);
	var diff;
	var checkdate =  gr.sys_created_on;//get created date from parent record
	var now = new GlideDateTime();//current datetime
//	var sched = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae');//cmn_schedule: 8-5 weekdays
	// lets use DurationCalculator
	var sched = new DurationCalculator();
	sched.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');	//cmn_schedule: 8-5 weekdays
	// startDate is undefined
//	diff = sched.duration(startDate, now);//difference between dates
	sched.calcScheduleDuration(checkdate, now);//difference between dates
	diff = sched.getSeconds();
	var noDays = diff / (60*60*24);
	gs.addInfoMessage("Seconds = " + diff + " Number of days: " + noDays);//display difference
	var newDateTime = new GlideDateTime(checkdate.getDisplayValue());
	
	// Add days in schedule
	var dc = new DurationCalculator('08fcd0830a0a0b2600079f56b1adb9ae');
	if (noDays >= 365) {
		newDateTime.addDays(180);
		gs.info("adding 180 days");
	
	}
	if (noDays <= 180)
		newDateTime.addDays(365);
	checkdate = newDateTime;
	gs.info("new checkdate = " + newDateTime + "\n");
}

 

 

And the debug output:

 

 

*** Script: brScript: Processing Contract: C37541, created: 2019-12-18 20:32:02
Background message, type:info, message: Seconds = 30374878 Number of days: 351.561087962963
*** Script: new checkdate = 2019-12-18 12:32:02

*** Script: brScript: Processing Contract: CNTR0009073, created: 2019-03-15 21:17:47
Background message, type:info, message: Seconds = 36783733 Number of days: 425.73765046296296
*** Script: adding 180 days
*** Script: new checkdate = 2019-09-11 14:17:47

*** Script: brScript: Processing Contract: CNTR0009078, created: 2019-03-15 21:17:47
Background message, type:info, message: Seconds = 36783733 Number of days: 425.73765046296296
*** Script: adding 180 days
*** Script: new checkdate = 2019-09-11 14:17:47

*** Script: brScript: Processing Contract: CNTR0009020, created: 2019-03-15 21:17:47
Background message, type:info, message: Seconds = 36783733 Number of days: 425.73765046296296
*** Script: adding 180 days
*** Script: new checkdate = 2019-09-11 14:17:47

*** Script: brScript: Processing Contract: CNTR0009070, created: 2019-03-15 21:17:48
Background message, type:info, message: Seconds = 36783732 Number of days: 425.7376388888889
*** Script: adding 180 days
*** Script: new checkdate = 2019-09-11 14:17:48

*** Script: brScript: Processing Contract: CNTR0009068, created: 2019-03-15 21:17:47
Background message, type:info, message: Seconds = 36783733 Number of days: 425.73765046296296
*** Script: adding 180 days
*** Script: new checkdate = 2019-09-11 14:17:47

...

 

 

 

Good luck on updating your BR script. I had to use DurationCalculator.

 

https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/c_DurationCalculatorAPI?n...

Samaksh Wani
Giga Sage

Hello @Desmo 

 

Plz mark my solution as Accept, if you got any help from it and Close the thread. It will help future readers to reach the post.

 

Regards,

Samaksh