UCCX script depending on previous script execution

Answered Question
Jul 17th, 2014
User Badges:

Hello,

 

So basically I am trying to write script whose execution will depend on if the same script was run in last half an hour. If the script was run in previous half an hour, a call should no be placed inside the script, otherwise it should. I tried to do it with session get/set info and Default Session Timeout* parameter set to 30 mins, but I am getting an error that the sesion is null and I can't get pass this step.

Script is HTTP triggered and I want to make sure it is run only once in half an hour, no matter if it gets triggered more often than that.

Any ideas on how to accomplish this?

 

Thanks for any suggestions,

 

 

Best regards

 

Goran

Correct Answer by Gergely Szabo about 3 years 1 month ago

Hi,

I am not quite sure I can follow you. Sessions are not bound to a contact and in this case we don't really care if they are active or idle.

This way: sessions are like temporary databases. You can store key-value pairs in these temporary databases. Since they are temporary, they disappear in a certain time. This must be handled in your application gracefully, however, one should not build an application around it. In your case, you should not really fiddle around with the default session expiry time.

I tossed together a script that does the following:

1. asks the system: hey, is there a session named "simpleDB"? Or, more preciselly, is there a session with the following mapping ID: "simpleDB"? If yes, we assume we can refer to it in a variable aptly named "session". If not, we will create a new session, store it in the variable named "session" (Get Session step)

2. next, we ask the system: is there a row in our database with the ID of "nanosLastTime"? Or, more precisely, is there a context attribute named "nanosLastTime" in the variable referred as "session"? Assign its value to a local variable named "nanosLastTime". (Get Session Info step).

3. let's see the outcome of the previous step: is it null (meaning there's no such session attribute)? If it's null, let's assign the value of the "nanosLastTime" local variable: System.nanoTime(), then map it to the session "session" and then save it (the first If step and its True branch). If it's not null, just continue to the next step.

4. Take the value of System.nanoTime() and assign it to the "nanosNow" variable.

5. Is (nanosNow - nanosLastTime) < desiredDelta, meaning did not the specified number of nanoseconds passed since the actual database record? If yes (true), then that's it, the script ends. If not (false), then let's just continue to the next step.

6. Update and save the session "simpleDB".

7. Do something useful.

 

Now some details:

- why this nanoTime construct: it's safe and it's just for this purpose, measuring the passed time between two moments. This is supposed to be safe even when the system clock is adjusted;

- why not use a mechanism to check whether the session mapped with an ID exist and use the default session timeout? Think universal.

Screenshots:







 

G.

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 5 (1 ratings)
Loading.
Gergely Szabo Thu, 07/17/2014 - 09:36
User Badges:
  • Green, 3000 points or more
  • Community Spotlight Award,

    Member's Choice, December 2015

Hi,

theoretically, the Session approach should work. Can you please describe what and how you tried to program and the exact error message.

Another approach would be to make a note in a database or in an XML document or just any plaintext document with the timestamp of the last script execution. Each time the application is triggered the script should first check that timestamp and if a half hour interval passed since then it should continue and stop otherwise.

G.

goranpilat Fri, 07/18/2014 - 01:14
User Badges:

Hi Gergely,

 

Thanks for your input. Yes, one approach would be with some outer file which is to be parsed always on the beginning of the execution, but I thought session would be easier.

I am triggering my script with HTTP trigger, and noticed on UCCX's Real Time Reporting screen that there is no session until an actual "place call" step is made. So on the beginning of the script I would like to put a step which will see if there is IDLE session  (from the previous script execution) on the system. If there is, means there has been successful execution in last half an hour and that script should stop.

But I'm not sure how (get session, get session info) to extract IDLE session from the system, I get session is null. If he can't extract IDLE session, I don't want it to break my script

 

Regards and thanks

 

Goran

 

Correct Answer
Gergely Szabo Fri, 07/18/2014 - 03:13
User Badges:
  • Green, 3000 points or more
  • Community Spotlight Award,

    Member's Choice, December 2015

Hi,

I am not quite sure I can follow you. Sessions are not bound to a contact and in this case we don't really care if they are active or idle.

This way: sessions are like temporary databases. You can store key-value pairs in these temporary databases. Since they are temporary, they disappear in a certain time. This must be handled in your application gracefully, however, one should not build an application around it. In your case, you should not really fiddle around with the default session expiry time.

I tossed together a script that does the following:

1. asks the system: hey, is there a session named "simpleDB"? Or, more preciselly, is there a session with the following mapping ID: "simpleDB"? If yes, we assume we can refer to it in a variable aptly named "session". If not, we will create a new session, store it in the variable named "session" (Get Session step)

2. next, we ask the system: is there a row in our database with the ID of "nanosLastTime"? Or, more precisely, is there a context attribute named "nanosLastTime" in the variable referred as "session"? Assign its value to a local variable named "nanosLastTime". (Get Session Info step).

3. let's see the outcome of the previous step: is it null (meaning there's no such session attribute)? If it's null, let's assign the value of the "nanosLastTime" local variable: System.nanoTime(), then map it to the session "session" and then save it (the first If step and its True branch). If it's not null, just continue to the next step.

4. Take the value of System.nanoTime() and assign it to the "nanosNow" variable.

5. Is (nanosNow - nanosLastTime) < desiredDelta, meaning did not the specified number of nanoseconds passed since the actual database record? If yes (true), then that's it, the script ends. If not (false), then let's just continue to the next step.

6. Update and save the session "simpleDB".

7. Do something useful.

 

Now some details:

- why this nanoTime construct: it's safe and it's just for this purpose, measuring the passed time between two moments. This is supposed to be safe even when the system clock is adjusted;

- why not use a mechanism to check whether the session mapped with an ID exist and use the default session timeout? Think universal.

Screenshots:







 

G.

 

 

 

goranpilat Mon, 07/21/2014 - 01:44
User Badges:

Hi Gergely,

exactly what I needed. Thanks a lot.

 

Regards

 

Goran

Actions

This Discussion