Countdown on pega platfrom

Hi pega friends,

I hope you’re doing well! I have a requirement in my Pega project where I need to implement a countdown timer that starts when a case is initiated and runs for one hour.

I’ve tried adding JavaScript code in a control and using it within a section, but I haven’t been able to get it working correctly. I was wondering if you might have any insights or suggestions on how to approach this? Any guidance would be greatly appreciated!

Thank you in advance, and looking forward to your response.

@SafoueneJ4378

Please check the pxSessionTimer to implement the countdown timer. I believe this could be a starting point to build your requirements.

@RameshSangili Thank you for your response, i found another way of doing it by using declare expressions.

Have a nice day.

@SafoueneJ4378

Can you please give a detailed step of your approach?

Thanks,

Sangeeth

@Sangeeth

Hello,

yes i created a proprety(for exemple: Timer (text) that is watched by a declare expression(.Timer) and i used the legacy mode, just click on action and use legacy declare expression, and for my case i am basing my count on the pxCreateDateTime of the case:
This is the expression used by the declare expression:

((@round(@DateTime.DateTimeDifference(@CurrentDateTime(),@addToDate(.pxCreateDateTime,“0”,“0”,getDataSystemSetting(“Trial”,“ExamTime”),“0”),“m”))<10?“0”:“”)+@round(@DateTime.DateTimeDifference(@CurrentDateTime(),@addToDate(.pxCreateDateTime,“0”,“0”,getDataSystemSetting(“Trial”,“ExamTime”),“0”),“m”)))+“:”+((@round(@DateTime.DateTimeDifference(@CurrentDateTime(),@addToDate(.pxCreateDateTime,“0”,“0”,getDataSystemSetting(“Trial”,“ExamTime”),“0”),“s”))-@round(@DateTime.DateTimeDifference(@CurrentDateTime(),@addToDate(.pxCreateDateTime,“0”,“0”,getDataSystemSetting(“Trial”,“ExamTime”),“0”),“m”))*60<10?“0”:“”)+(@round(@DateTime.DateTimeDifference(@CurrentDateTime(),@addToDate(.pxCreateDateTime,“0”,“0”,getDataSystemSetting(“Trial”,“ExamTime”),“0”),“s”))-@round(@DateTime.DateTimeDifference(@CurrentDateTime(),@addToDate(.pxCreateDateTime,“0”,“0”,getDataSystemSetting(“Trial”,“ExamTime”),“0”),“m”))*60))

This is and explanation of the steps of this expression:

Minutes Calculation
a. @DateTime.DateTimeDifference(@CurrentDateTime(), @addToDate(.pxCreateDateTime, …), “m”)
What it does:
@CurrentDateTime(): Gets the current date and time.
.pxCreateDateTime: The start time of the session .
@addToDate(.pxCreateDateTime, “0”, “0”, getDataSystemSetting(“Trial”, “ExamTime”), “0”): Adds the exam duration (retrieved via getDataSystemSetting) to .pxCreateDateTime to calculate the end time.
@DateTime.DateTimeDifference(…): Calculates the difference between the current time and the end time in minutes (“m”).
b. @round(…)
What it does: Rounds the minute difference to the nearest whole number.
c. @round(… < 10 ? “0” : “”) + …
What it does: Formats single-digit minutes with a leading zero:
If the rounded minute value is less than 10, it prepends a “0”.
Otherwise, it leaves the value as is.
Seconds Calculation
a. @DateTime.DateTimeDifference(@CurrentDateTime(), @addToDate(…), “s”)
What it does: Similar to the minutes calculation, but it calculates the difference in seconds (“s”).
b. - @round(@DateTime.DateTimeDifference(…, “m”)) * 60
What it does: Converts the minute part back into seconds (minutes × 60) and subtracts it from the total seconds. This gives the remaining seconds.
c. … < 10 ? “0” : “” + …
What it does: Formats single-digit seconds with a leading zero:
If the remaining seconds are less than 10, it prepends a “0”.
Otherwise, it leaves the value as is.

And then you can call the proprety Timer on your section to get your timer.