Legacy Dev Forum Posts

 View Only

Sign Up

Transform Array to String - Timezone Datetime

  • 1.  Transform Array to String - Timezone Datetime

    Posted 06-05-2025 18:42

    Charaf | 2021-02-02 13:56:35 UTC | #1

    Hello,

    I am, using the following API in order to get the current datetime : As a result :

    As you can see, the datetime doesn't include the offset related to timezone.

    I found another API to interact with that gave me a list of timezones :

    After some filtering because I am interested on only Paris Timezone, I got the result below :

    For the response, I applied an expression to filter the results :

    Since currently scripts cannot read data from JSON array, I am wondering if there is a way to transform the array into a single string, So I can read it inside a script and apply some formulas to take into account the timezone when getting a cuurent datetime...

    Below tye contracts output :

    Any ideas please ?

    Kind Regards,

    CHARAF


    Jerome.Saint-Marc | 2021-02-02 15:05:57 UTC | #2

    Hello,

    You could modify your Output Contract, defining Offset as an Integer. And, to get the first element from the array, you could use the successTemplateUtils.firstFromArray macro. Using a Response Configuration like this:

    { "translationMap": { "offsetarray": "$.entities[?(@.id == 'Europe/Paris')].offset" }, "successTemplate": "{\"Offset\": ${successTemplateUtils.firstFromArray(\"${offsetarray}\")} }" }

    Also note that in Utilities API, there is an endpoint to get the timezone: GET /api/v2/timezones

    But as you mention that you are trying to display the current date/time in a Script, you could maybe use a Script built-in variable, if it fits what you need. {{Scripter.Agent Call Start Time}} — time when the call was connected to the agent. This one would correspond to the date/time the conversation is answered by the agent (voice, chat, ...). This will save 2 API requests (Call to Data Action) - which are taken into account in API rate limits. It seems to take into account local computer timezone - my Genesys Cloud sandbox is in Ireland and my computer is on France timezone. It was displaying this, when my agent accepted a chat and displayed the corresponding script: *02/02/2021 3:52:23 pm (+01:00)*

    Regards,


    Charaf | 2021-02-15 16:28:15 UTC | #3

    Hi @Jerome.Saint-Marc

    Thank you for your detailled answer.

    You are right, I will follow your recommandation in order to save some API Calls.

    The built-il variable {{Scripter.Agent Call Start Time}} took into account the local timezone. Would it be possible to get only the time in UTC+0 instead ?

    Actually, I found a solution using Account of services in Microsoft Dynamics side, which will adapt the time in the local timezone of the Agent, but the condition is to send the time in UTC+0.

    Kind Regards,


    Jerome.Saint-Marc | 2021-02-15 16:35:33 UTC | #4

    Hello,

    I don't understand your comment. {{Scripter.Agent Call Start Time}} is already displayed in local timezone of the agent.

    Regards,


    Charaf | 2021-02-15 16:38:58 UTC | #5

    Hello,

    Yes but now I would like to get the datetime in UTC+0 and not in local timezone of the agent.

    Regards,


    Jerome.Saint-Marc | 2021-02-16 07:09:32 UTC | #6

    Hello,

    To avoid invoking a Data Action, if that fits your use case, you could get the UTC date/time in your Architect flow, and "attach" it as a participant data/attribute (using the Set Participant Data block). You can then retrieve it in the Script, defining a variable with the name of your Participant Data/attribute - with Input toggle enabled. This will tell Script to read the value from the participant data.

    In Architect flow, you can get the UTC date/time using the GetCurrentDateTimeUtc function or if it is an Inbound Call flow, using the Flow.StartDateTimeUtc built-in variable. https://developer.mypurecloud.com/forum/t/converting-utc-time-to-local-time/2370

    And just in case this can help as well, note that you can display external web pages inside a Script using Web Page component (if you have web pages - that can be embedded inside an iframe - and which shows date/time in different timezones). https://help.mypurecloud.com/articles/web-page-script-component/

    Regards,


    Charaf | 2021-02-16 13:45:20 UTC | #7

    Hi @Jerome.Saint-Marc,

    Thank you again for your answer.

    I was using GetCurrentDateTimeUtc() in Architect and it was working fine. But, the problem is to get the time (UTC) when the call started with the agent, instead of the the time the call hit the call flow.

    Regards,


    Jerome.Saint-Marc | 2021-02-16 16:08:26 UTC | #8

    I don't think there is a way for this. Script does not have date/time functions like what exists in Architect. You could try to parse and slice/split the {{Scripter.Agent Call Start Time}} using Dynamic Script variables, as the delta to GMT (and UTC) appears at the end -> *02/02/2021 3:52:23 pm (+01:00)* was for (UTC+1). But I doubt it is easy to do or doable - extracting +01:00 is, but then converting it to a number and subtract it from the current time is probably not.

    For this, you might have to create a Genesys Cloud Data Action invoked from the Script, to extract the startTime (ringing) of the agent's participant in the conversation context (Get Conversation with /api/v2/conversations/{conversationId}). It would not be the answered time but the ringing start time - the Script is loaded on ringing (not answered/established). It would retrieve a format like this: "2021-02-16T15:57:46.661Z" With startRingingTime as String variable in your output contract, the Response configuration could look something like this:

    { "translationMap": { "ArrayStartTime": "$.participants[?(@.purpose ==\"agent\")].startTime" }, "translationMapDefaults": { "ArrayStartTime": "[]" }, "successTemplate": "{\"startRingingTime\": ${successTemplateUtils.firstFromArray(\"${ArrayStartTime}\",\"0\")} }" }

    Regards,


    Charaf | 2021-02-17 15:20:17 UTC | #9

    Thank you @Jerome.Saint-Marc

    I tested the Data Action you described above and it is working fine.

    Kind Regards,

    CHARAF


    Aakanksha_Goyal | 2021-03-02 17:57:00 UTC | #10

    Jerome.Saint-Marc, post:2, topic:9869
    ${successTemplateUtils.firstFromArray(\"${offset_array}\")}

    Hi Jerome,

    I have a similar follow-up question on this, that if you are getting an array response from the data action / API response it is anyway possible to convert that to a comma-separated string in the success template somehow so that it can be passed on to the script?

    This is how my current response looks like, the array length here can change.

    I want to call this data action from my script, but since we cannot use the array variables in script, I was thinking if we can concatenate the array elements into a single string.

    Any help is appreciated.

    Thank you, Aakanksha


    Jerome.Saint-Marc | 2021-03-04 13:27:16 UTC | #11

    Hello Aakanksha,

    Sorry for the delay, I was trying different Data Action configurations to find how to transform the array into a string - as I am not practicing the Script part often.

    Please see this other post for a method proposed by Jason, and a different one from me: https://developer.mypurecloud.com/forum/t/get-wrapup-notes/10167

    Regards,


    Aakanksha_Goyal | 2021-03-04 16:23:50 UTC | #12

    Thank you so much @Jerome.Saint-Marc. This is very helpful. I will try what you have suggested on this thread. !!


    system | 2021-04-04 16:23:54 UTC | #13

    This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.


    This post was migrated from the old Developer Forum.

    ref: 9869