Calculating TimeSpan using the working schedule

Hi,

Using the code below, I would like to calculate the number of minutes between the currentmoment and creation of the ticket.

After this I am saving this in a Timespan field.

     DateTime createdAt=ticket.getValue("created_at").toDateTime();
     Integer secondsToQuote=getCurrentDateTime().diff(createdAt);
     Float minsToQuoteFloat = Float(secondsToQuote.toString())/60;
     String minsToQuote=minsToQuoteFloat.toString(0);

     ticket.setValue("x_time_to_quote",minsToQuote);
     ticket.save();

But unfortunately, this is not related to the working hours entered at the priority.

Is there a standard function I can use for this kind of calculation, since it is not obvious to do this calculation manually.

thankx in advance!

 

niels

RE: Calculating TimeSpan using the working schedule

I believe the class WeekSchedule in CRMScript is what you are looking for :)

Sverre

Av: Sverre Hjelm 6. jun 2017

RE: Calculating TimeSpan using the working schedule

To elaborate on Sverre's answer:

WeekSchedule ws;
ws.loadFromPriority(ticket.getValue("priority").toInteger());
TimeSpan ts = ws.getDifference(DateTime(ticket.getValue("createdAt")), getCurrentDateTime());

ticket.setValue("x_time_to_quote", ts.toMinutes().toString());
ticket.save();

Should do the trick

Av: Simen Mostuen Iversen 6. jun 2017

RE: Calculating TimeSpan using the working schedule

Sverre, Simon, 

 

This information was spot on!

Thanx guys!

 

Niels

Av: Niels van Broekhoven 6. jun 2017

RE: Calculating TimeSpan using the working schedule

Forgot to add the 

 

ws.loadFromPriority(ticket.getValue("priority").toInteger());

Av: Simen Mostuen Iversen 6. jun 2017

RE: Calculating TimeSpan using the working schedule

Hi Simen, 

I just got started, and indeed wondered how this would be loaded, so thanx again!

Niels

Av: Niels van Broekhoven 6. jun 2017

RE: Calculating TimeSpan using the working schedule

Hi Simen,

I have built this and it works. But while testing I saw that in some cases the difference is exact 2 hours of what is should be.

Is there some Time-zone influence perhaps done in this calculation?

/Niels

Av: Niels van Broekhoven 14. jun 2017