We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

what is wrong in below script?

Sathwik1
Tera Guru

 

What is wrong in below script?

 
var schedule = new GlideSchedule(inputs.schedule_sys_id);
var start = new GlideDateTime(inputs.starting_date);
var end = new GlideDateTime(inputs.ending_date);
var duration = schedule.duration(start, end);
outputs.duration = duration;
 
Here duration value is fine, perfectly caluclating.. but in backend table it is not setting up.. ending up as "return type is not valid"
 
In the above..my ouput field type is duration... in bacend table as well.. field type is duration.
 
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@Sathwik1 

try this

var schedule = new GlideSchedule(inputs.schedule_sys_id);
var start = new GlideDateTime(inputs.starting_date);
var end = new GlideDateTime(inputs.ending_date);

var dur = schedule.duration(start, end);
outputs.duration = new GlideDuration(dur);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

@Sathwik1 

The error "Invalid return type encountered" usually indicates the consumer of outputs.duration doesn't support the object being returned, rather than an issue with schedule.duration() itself.

Anukur solution worked, thanks @marianbruma  for your efforts.

Tanushree Maiti
Tera Patron

Hi @Sathwik1 

 

Refer these two posts:

https://www.servicenow.com/community/developer-forum/script-to-convert-string-to-duration/m-p/244236...

https://www.servicenow.com/community/developer-forum/date-conversion-problems/m-p/1698988

 var starting_date = new GlideDateTime(inputs.start_date);
     var ending_date = new GlideDateTime(inputs.end_date);
     var time_zone_adj = new GlideTime();
     time_zone_adj.setValue("08:00:00");
     starting_date.add(time_zone_adj);
     ending_date.add(time_zone_adj);

     var schedule = new GlideSchedule(inputs.schedule_sys_id);
     var duration = schedule.duration(starting_date, ending_date);
     //outputs.message = "Schedule Duration: " + duration.getDurationValue();

     outputs.starting_date = starting_date;
     outputs.ending_date = ending_date;
     outputs.duration = duration;

 

Try both.

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Hi @Sathwik1 

 

Did you try the solution mentioned in the link.

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti