Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to calculate duration between two dates in business rule?

chenglo
Tera Contributor

Hi Experts,

I would to have your support for my issue. I'm creating a business rule to get data into another table for reports purpose. I want to calculate the duration between two dates that I have in my table. I'm currently using gs.dateDiff(); but seems the output isn't correct. Can any one please help me on this?

eg: opendate is "28-Aug-2018" // get from creation date

      supdate is "30-Aug-2018" // I get from supply team approval date

      Result = 14 // which is the number of days but in fact it doesn't correct. It's only 2 days, so the result I need to get 2.

 

(function executeRule(current, previous /*null when async*/) {
 
 // Add your code here
  
  if(current.u_vendor_aging_r_s =='Supply Completed'){
   
   // Query from sysapproval and insert to the table
   var recSysapproval = new GlideRecord('sysapproval_approver');
   recSysapproval.addQuery('sysapproval', current.sys_id);
   recSysapproval.addQuery('state', "Approved");
   recSysapproval.orderByDesc('sys_created_on');
   recSysapproval.setLimit(1);
   recSysapproval.query();
   
   while (recSysapproval.next()){
    var grvendor = new GlideRecord('u_vendor_master_data_aging_report');
    
    grvendor.initialize();
    
    grvendor.u_tsc_number = current.sys_id;
    grvendor.u_supply_appr_date = recSysapproval.sys_updated_on;
    grvendor.u_supply_approver = recSysapproval.approver.getDisplayValue();
    grvendor.u_company_code = current.variables.Company.getDisplayValue();
    grvendor.u_vendor_name = current.variables.vendor_name.getDisplayValue();
    grvendor.u_vendor_code = current.variables.vendor_code_by_TSC.getDisplayValue();
    grvendor.u_currency = current.variables.Currency.getDisplayValue();
    grvendor.u_type = current.variables.re_type.getDisplayValue();
    grvendor.u_request_date = current.variables.re_date.getDisplayValue();
    grvendor.insert();
    
    gs.addInfoMessage('The record has been inserted to Table');

     var opendate = current.u_request_date.getDisplayValue();
     var supdate = grvendor.u_supply_appr_date.getGlideObject();
     grvendor.u_aging_sup = gs.dateDiff(supdate.getDisplayValue(),opendate.getDisplayValue(),false);
     grvendor.update();
     gs.addInfoMessage('Effected the Aging');

    }
  
 })(current, previous);

 

Thanks for your support,

Cheng

1 ACCEPTED SOLUTION

HI,

I am confused here with this var opendate = current.u_request_date.getDisplayValue();

 

 from where u_request comes from.

Thanks,
Ashutosh

View solution in original post

11 REPLIES 11

SaiRaviKiran Ak
Tera Guru

May I know what are the values that you are getting in var opendate and   var supdate ?

Hi Ravi,

- opendate: I get from catalog variable which is request date "28-Aug-2018"

- supdate: I get from table sysapproval_approver which is an approval date of supply team which is "30-Aug-2018".

and I need to get a duration between these two dates, and store in u_aging_sup field which datatype is integer

 

Regards,

Cheng

Try replacing grvendor.u_aging_sup = gs.dateDiff(supdate.getDisplayValue(),opendate.getDisplayValue(),false);

with

 grvendor.u_aging_sup = gs.dateDiff(supdate,opendate,false);

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

 

Are this variables Date type or dateTime type.


Thanks,
Ashutosh