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.

calculating business days

sonita
Giga Guru

How to calculate business days on a catalog form?

1 ACCEPTED SOLUTION

Soni,



  Add alert in your call back function as shown below and let me know what you see.


    Here is your onChange client script on Needed By   variable:


function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  g_form.setDisplay('title', false); // put in the variable name you want to show or hide according needed by date


  return;


  }


  var ga = new GlideAjax('ValidateNeededBy');


  ga.addParam('sysparm_name','validateDate');


  ga.addParam('sysparm_needed_by',newValue);


  ga.getXML(callBack);



  function callBack(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


alert(answer);


  if(parseInt(answer)>=27){


  g_form.setDisplay('title', false); // put in the variable name you want to show or hide according needed by date


  }


  else{


  g_form.setDisplay('title', true); // put in the variable name you want to show or hide according needed by date



  }


  }


}


View solution in original post

23 REPLIES 23

Abhinay Erra
Giga Sage

Soni,



As Mike suggested , use schedules for this. Then you could use duration calculator class to calculate days according to your schedule


Here is a sample script of it.


var dc = new DurationCalculator();  


  dc.setSchedule('4478d9d969bfa100f336d9fc410262ec');   //put in the sys_id of the schedule in your instance like 8-5 weekdays


  var dur = dc.calcScheduleDuration(inc.end_time, gs.nowDateTime());   //calculate duration according to your schedule




Thanks,


Abhinay



PS: Hit like, Helpful or Correct depending on the impact of the response


Actually I have a date field. if the selected date is less than 3 business days another field should be shown


Soni,



  here is your script for that. You can write this in a Business rule


Script:


var dc = new DurationCalculator();


dc.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); // this is the sys_id of 8-5 weekday schedule in your instance. You can put in the sys_id of the schedule you want to use


  dc.setTimeZone('GMT');


  var dur = dc.calcScheduleDuration(gs.nowDateTime(),put in the date time field you want to check)/3600;


if(dur>27){ //each working day is 9 hrs, so 3 working days is 9*3 27 hrs


gs.addInfoMessage("duartion is greater than 3 days");


}



Thanks,


Abhinay



PS: Hit like, Helpful or Correct depending on the impact of the response


Thanks Abhinay E , but how am I able to write a business rule on a catalog item form?


Soni,



    What exactly is your requirement? Can you please elaborate your requirement.




Thanks,


Abhinay