- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2019 06:32 AM
Below script from executeSample function i need to take date2 variable value and pass it to addSchedule2 function , please help me your suggestions.
gs.include('DurationCalculator');
executeSample();
function executeSample(){
var dc = new DurationCalculator();
addSchedule(dc);
var created = '22-Nov-2019 11:25:31';
var gdt = new GlideDateTime(created);
dc.setStartDateTime(gdt);
if(!dc.calcDuration(2*8*3600)){ // 2 days
gs.log("*** Error calculating duration");
return;
}
gs.log("calcDuration no schedule: " + dc.getEndDateTime());
var date2 = dc.getEndDateTime();
gs.print(dc.getEndDateTime());
}
function addSchedule(durationCalculator){
// Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.
var scheduleName = "8-5 weekdays";
var grSched = new GlideRecord('cmn_schedule');
grSched.addQuery('name', scheduleName);
grSched.query();
if(!grSched.next()){
gs.log('*** Could not find schedule "'+ scheduleName +'"');
return;
}
durationCalculator.setSchedule(grSched.getUniqueValue(),"IST");
}
gs.include('DurationCalculator');
executeSample2();
function executeSample2(){
var dc = new DurationCalculator();
addSchedule2(dc);
var created = date2;
var gdt = new GlideDateTime(created);
dc.setStartDateTime(gdt);
if(!dc.calcDuration(2*8*3600)){ // 2 days
gs.log("*** Error calculating duration");
return;
}
gs.log("calcDuration no schedule: " + dc.getEndDateTime());
var date3 = dc.getEndDateTime();
gs.print(dc.getEndDateTime());
}
function addSchedule2(durationCalculator){
// Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.
var scheduleName = "8-5 weekdays";
var grSched = new GlideRecord('cmn_schedule');
grSched.addQuery('name', scheduleName);
grSched.query();
if(!grSched.next()){
gs.log('*** Could not find schedule "'+ scheduleName +'"');
return;
}
durationCalculator.setSchedule(grSched.getUniqueValue(),"IST");
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2019 06:51 AM
You can use the function to set the value of a variable outside the context of the function, then you can use it as an argument in another function.
gs.include('DurationCalculator');
var date2 = executeSample(); //set the variable value by calling the function
function executeSample(){
var dc = new DurationCalculator();
addSchedule(dc);
var created = '22-Nov-2019 11:25:31';
var gdt = new GlideDateTime(created);
dc.setStartDateTime(gdt);
if(!dc.calcDuration(2*8*3600)){ // 2 days
gs.log("*** Error calculating duration");
return;
}
gs.log("calcDuration no schedule: " + dc.getEndDateTime());
return = dc.getEndDateTime();
gs.print(dc.getEndDateTime());
}
function addSchedule(durationCalculator){
// Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.
var scheduleName = "8-5 weekdays";
var grSched = new GlideRecord('cmn_schedule');
grSched.addQuery('name', scheduleName);
grSched.query();
if(!grSched.next()){
gs.log('*** Could not find schedule "'+ scheduleName +'"');
return;
}
durationCalculator.setSchedule(grSched.getUniqueValue(),"IST");
}
gs.include('DurationCalculator');
executeSample2(date2); //send the variable in as a parameter to the function
function executeSample2(date){ //add the variable as an argument
var dc = new DurationCalculator();
addSchedule2(dc);
var created = date;
var gdt = new GlideDateTime(created);
dc.setStartDateTime(gdt);
if(!dc.calcDuration(2*8*3600)){ // 2 days
gs.log("*** Error calculating duration");
return;
}
gs.log("calcDuration no schedule: " + dc.getEndDateTime());
var date3 = dc.getEndDateTime();
gs.print(dc.getEndDateTime());
}
function addSchedule2(durationCalculator){
// Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.
var scheduleName = "8-5 weekdays";
var grSched = new GlideRecord('cmn_schedule');
grSched.addQuery('name', scheduleName);
grSched.query();
if(!grSched.next()){
gs.log('*** Could not find schedule "'+ scheduleName +'"');
return;
}
durationCalculator.setSchedule(grSched.getUniqueValue(),"IST");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2019 06:51 AM
You can use the function to set the value of a variable outside the context of the function, then you can use it as an argument in another function.
gs.include('DurationCalculator');
var date2 = executeSample(); //set the variable value by calling the function
function executeSample(){
var dc = new DurationCalculator();
addSchedule(dc);
var created = '22-Nov-2019 11:25:31';
var gdt = new GlideDateTime(created);
dc.setStartDateTime(gdt);
if(!dc.calcDuration(2*8*3600)){ // 2 days
gs.log("*** Error calculating duration");
return;
}
gs.log("calcDuration no schedule: " + dc.getEndDateTime());
return = dc.getEndDateTime();
gs.print(dc.getEndDateTime());
}
function addSchedule(durationCalculator){
// Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.
var scheduleName = "8-5 weekdays";
var grSched = new GlideRecord('cmn_schedule');
grSched.addQuery('name', scheduleName);
grSched.query();
if(!grSched.next()){
gs.log('*** Could not find schedule "'+ scheduleName +'"');
return;
}
durationCalculator.setSchedule(grSched.getUniqueValue(),"IST");
}
gs.include('DurationCalculator');
executeSample2(date2); //send the variable in as a parameter to the function
function executeSample2(date){ //add the variable as an argument
var dc = new DurationCalculator();
addSchedule2(dc);
var created = date;
var gdt = new GlideDateTime(created);
dc.setStartDateTime(gdt);
if(!dc.calcDuration(2*8*3600)){ // 2 days
gs.log("*** Error calculating duration");
return;
}
gs.log("calcDuration no schedule: " + dc.getEndDateTime());
var date3 = dc.getEndDateTime();
gs.print(dc.getEndDateTime());
}
function addSchedule2(durationCalculator){
// Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.
var scheduleName = "8-5 weekdays";
var grSched = new GlideRecord('cmn_schedule');
grSched.addQuery('name', scheduleName);
grSched.query();
if(!grSched.next()){
gs.log('*** Could not find schedule "'+ scheduleName +'"');
return;
}
durationCalculator.setSchedule(grSched.getUniqueValue(),"IST");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2019 07:01 AM
Hi David,
thank you for your quick response, if i execute background script for this date2 and date3 both are coming same value , it should not come same value, based on date2 value date3 value has to change rgt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2019 07:11 AM
If you're in background scripts you might have to wrap the whole thing up in a function eg:
runFunctions()
function runFunctions(){
var date2 = executeSample();
executeSample2(date2);
}
function executeSample(){
//code
}
etc etc
I haven't tested the code, it's just a method you can use. To have a variable from one function context available in another you can either populate a variable in the global context or call the other function from within the first and pass through the variable as an argument eg:
function executeSample(){
var dc = new DurationCalculator();
addSchedule(dc);
var created = '22-Nov-2019 11:25:31';
var gdt = new GlideDateTime(created);
dc.setStartDateTime(gdt);
if(!dc.calcDuration(2*8*3600)){ // 2 days
gs.log("*** Error calculating duration");
return;
}
gs.log("calcDuration no schedule: " + dc.getEndDateTime());
var date2 = dc.getEndDateTime();
executeSample2(date2);
gs.print(dc.getEndDateTime());
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2019 07:19 AM
thank you David once again it is working as expected.