Struggle with the 'null' result in my Data Action

Hi,

We use a Data Action to retrieve the amount of transfers that a conversation had. This value we show in the agent script so that the agent can see that the call has been transferred.

Input Contract

{
  "title": "input",
  "type": "object",
  "properties": {
    "conversationId": {
      "type": "string"
    }
  },
  "additionalProperties": true
}

Output Contract

{
  "title": "output",
  "type": "object",
  "properties": {
    "nTransferred": {
      "type": [
        "integer",
        "null"
      ]
    }
  },
  "additionalProperties": true
}

Request

{
  "requestUrlTemplate": "/api/v2/analytics/conversations/${input.conversationId}/details",
  "requestType": "GET",
  "headers": {},
  "requestTemplate": "${input.rawRequest}"
}

Response

{
  "translationMap": {
    "nTransferredArrayAsStr": "$.participants[*].sessions[*].metrics[?(@.name=='nTransferred')].value"
  },
  "translationMapDefaults": {
    "nTransferredArrayAsStr": "[]"
  },
  "successTemplate": "#set ($nTransferredArray = $nTransferredArrayAsStr.replace(\"[\", \"\").replace(\"]\", \"\").replace(\" \", \"\").split(\",\")) {\"nTransferred\":$math.getTotal(${nTransferredArray})}"
}

Now if there have been some transfers it works like a charm:

But on the first transfer the value is not there so I receive an error, I have been searching and trying but cannot seem to find the solution.

Error message: 10. Apply output transformation: Transform failed to process result using 'successTemplate' template due to error:'Reference $math.getTotal(${nTransferredArray}) evaluated to null when attempting to render at successTemplate[line 1, column 131]' Template:'#set ($nTransferredArray = $nTransferredArrayAsStr.replace("[", "").replace("]", "").replace(" ", "").split(",")) {"nTransferred":$math.getTotal(${nTransferredArray})}'.

Is there anyone that has a suggestion?
Thanks in advance!

BR,
Rob

I am guessing that the issue is that you are asking $math.getTotal to add up the values in an empty array, and it isn't happy about that. The first thing I would try is to give it a value to work with

"translationMapDefaults": {
"nTransferredArrayAsStr": "[0]"
},

If that doesn't work, please post an example of a working and non working response from analytics so people on the forum can have something to experiment with.

--Jason

Hi Jason,

Thanks for taking the time to have a look at my question.

I did try "nTransferredArrayAsStr": "[0]" also but that doesn't work.
I was experimenting with $nTransferredArrayAsStr.isEmpty() or #if( "!$nTransferredArrayAsStr" == "" ) but I can't get the syntax to work.

The problem is that for a conversation that has been transferred there is one or more nTransferred values in the metrics like in the example below.

      "metrics": [
        {
          "emitDate": "2024-01-24T09:45:03.274Z",
          "name": "nOutbound",
          "value": 1
        },
        {
          "emitDate": "2024-01-24T09:45:05.139Z",
          "name": "tContacting",
          "value": 1865
        },
        {
          "emitDate": "2024-01-24T09:45:08.576Z",
          "name": "tDialing",
          "value": 3437
        },
        {
          "emitDate": "2024-01-24T09:49:31.769Z",
          "name": "nBlindTransferred",
          "value": 1
        },
        {
          "emitDate": "2024-01-24T09:49:31.769Z",
          "name": "nTransferred",
          "value": 1
        },

In the case that the calls hasn't been transferred this value is not in the result JSON of the request so the string / array is empty (not there actually).

Thanks.

BR,
Rob

Hi Jason,

Is there a way to state if $nTransferredArrayAsStr is empty or null we give it a value 0 or we give the variable $nTransferredArray the value 0 in the success template?

I have tried several options and read multiple sites and documents but cannot find the correct syntax.

BR,
Rob

Hello,

The following appears to work for me (just tried it):

{
  "translationMap": {
    "nTransferredArrayAsStr": "$.participants[*].sessions[*].metrics[?(@.name=='nTransferred')].value"
  },
  "translationMapDefaults": {
    "nTransferredArrayAsStr": "[]"
  },
  "successTemplate": "#set ($nTransferredArray = $nTransferredArrayAsStr.replace(\"[\", \"\").replace(\"]\", \"\").replace(\" \", \"\").split(\",\")) {\"nTransferred\": #if( $nTransferredArray.get(0) == '' )0#else$math.getTotal(${nTransferredArray})#end }"
}

Using split to get the nTransferredArray: as the string used in split function is empty, it means the output will be an array with one element = empty string (just like with a scripting language).
So I have a added a velocity test (if/else/end) to check if the first element is empty string, and if so, use 0 as output value.

Regards,

1 Like

Hi Jerome,

You rock!
It now works as should thank you very very much, both for the solution and for the lesson in response writing :slight_smile:

Have a great evening!
BR,
Rob

1 Like

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