Server side client script to calculate date difference between two fields on a form using ATF.

Deepa17
Giga Contributor

Hello, 

I need to calculate the time duration between two fields using ATF server side script test step. How can I do that please help me on that. 

I am using the below code to calculate.

  1. TimeDiff();  
  2.  
  3.   function getTimeDiff(){  
  4.   var startDate = current.u_start_date.getGlideObject();  
  5.   var endDate = current.u_end_date.getGlideObject();  
  6.  
  7.   current.u_duration = gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);  
  8.  
  9.   }  

But I don't know how to use this code in server side script test step code.

Please help me out with this.

 

Thanks,

Deepa

4 REPLIES 4

rajesh9885
Mega Guru

Is there a button which trigger this?

use the same in the button.

You can call a script include from the button

No, ATF (Automated Testing Framework) will automatically run these steps.

Ankush Agrawal
ServiceNow Employee

Hi Deepa

You just need to replace the object 'current' with a GlideRecord object where you want to use it, e.g.:

function getTimeDiff(grTable){  
  var startDate = grTable.u_start_date.getGlideObject();  
  var endDate = grTable.u_end_date.getGlideObject();  
 
  return gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);  
 
  }  

var grTable = new GlideRecord(<nameOfTable>);
grTable.get(<sysIDOfTheRecord>);

var duration = TimeDiff(grTable);  
 
  

--
Best Regards 
Ankush

P.S. Please mark helpful/correct as appropriate to help the fellow community member in identifying the relevant answers.

This worked perfectly.
Thank you 🙂