IF No Result or More Than 1 Result

I am just sorting through my (hopefully) last issue on my workflow for connecting Dialpad with our PSA. At this stage of the workflow, I’m searching for any open tickets belonging to a contact and using an IF to determine the next steps. Originally I had it set up so that if more than 1 ticket was found, it would create a new ticket and if only 1 ticket was found, it updates the existing ticket. This didn’t take into account if there were no open tickets and so now I’m faced with trying to tackle both scenarios through one IF. If there are no open tickets, I get this result from the endpoint:

[
   {
      "tickets":[
         
      ],
      "meta":{
         "total_pages":1,
         "page":1
      }
   }
]

If I get more than one result, I get this from the endpoint:

[
    {
       "tickets":[
          {
             "id":111,
             "number":1234,
             "subject":"Test",
             "created_at":"2021-04-07T10:33:21.818-06:00",
             "customer_id":1111,
             "customer_business_then_name":"Not So Clever Admin",
             "due_date":"2021-04-10T10:33:21.809-06:00",
             "resolved_at":null,
             "start_at":null,
             "end_at":null,
             "location_id":null,
             "problem_type":"Windows",
             "status":"New",
             "ticket_type_id":null,
             "properties":{
                
             },
             "user_id":222,
             "updated_at":"2021-04-07T10:33:21.958-06:00",
             "pdf_url":null,
             "priority":"2 Normal",
             "comments":[
                {
                   "id":141805757,
                   "created_at":"2021-04-07T10:33:21.744-06:00",
                   "updated_at":"2021-04-07T10:33:21.827-06:00",
                   "ticket_id":111,
                   "subject":"Initial Issue",
                   "body":"Ignore",
                   "tech":"Clever Admin - MT",
                   "hidden":false,
                   "user_id":24223
                }
             ],
             "user":{
                "id":666,
                "email":"[email protected]",
                "full_name":"Clever Admin - MT",
                "created_at":"2015-11-27T13:15:09.066-07:00",
                "updated_at":"2021-04-06T21:49:04.186-06:00",
                "group":"Admins",
                "admin":true,
                "color":"DEBB27"
             }
          },
          {
             "id":222,
             "number":2222,
             "subject":"Test",
             "created_at":"2021-04-07T10:32:31.049-06:00",
             "customer_id":1111,
             "customer_business_then_name":"Not So Clever Admin",
             "due_date":"2021-04-10T10:32:31.038-06:00",
             "resolved_at":null,
             "start_at":null,
             "end_at":null,
             "location_id":null,
             "problem_type":"Windows",
             "status":"New",
             "ticket_type_id":null,
             "properties":{
                
             },
             "user_id":666,
             "updated_at":"2021-04-07T10:32:31.715-06:00",
             "pdf_url":null,
             "priority":"2 Normal",
             "comments":[
                {
                   "id":141805585,
                   "created_at":"2021-04-07T10:32:30.973-06:00",
                   "updated_at":"2021-04-07T10:32:31.681-06:00",
                   "ticket_id":111,
                   "subject":"Initial Issue",
                   "body":"Ignore",
                   "tech":"Clever IT - Moez Tharani",
                   "hidden":false,
                   "user_id":24223
                }
             ],
             "user":{
                "id":666,
                "email":"[email protected]",
                "full_name":"Clever Admin - MT",
                "created_at":"2015-11-27T13:15:09.066-07:00",
                "updated_at":"2021-04-06T21:49:04.186-06:00",
                "group":"Admins",
                "admin":true,
                "color":"DEBB27"
             }
          }
       ],
       "meta":{
          "total_pages":1,
          "page":1
       }
    }
 ]

Is it possible for me to perform an IF against both scenarios and basically “true” if there is only 1 ticket result and “false” if there is more than 1 or none?

In a Function node:

return [ { json: { oneTicketReturned: items[0].json.tickets.length === 1 } } ];

In an If node:

{{$json["tickets"].length}} equal to 1 (number)

@ivov’s example above shows one of the main ways you can evaluate if something doesn’t exist with If node - I have flagged however that evaluating if something doesn’t exist is a bit tricky at times in n8n.

Some things below that might be useful:

  • Each object/ array in the expression editor can be referenced in your expression via the grey dot next to it’s name (grey dot > “Raw Values”). In the example above, you could click on that, then simply add .length to the variable that gets added. GIF Below:

get object (1)

  • The .length method works in this specific case but there is also a semi-common case where the key itself is not present in the “does not exist case”. More specifically, if the “doesn’t exist” case did not have a tickets object. In this scenario, I’m using a Bool condition and setting it up with a “does exist case” (so where {{$json["tickets"]}} “Equal to” True). So when you’d pass in the “doesn’t exist” case; $json["tickets"] would evaluate to false in a Bool condition. Hope that makes sense - can clarify if not!
1 Like

Yes, thank you! I did run into that issue where tickets doesn’t exist, so combining this with a length count got me what I needed.

1 Like

That is amazing to hear! Glad that this makes sense. We will be looking in future how to make conditional logic/ evaluating if something “isnt” (and all the various ways that can occur) more intuitive!

For the community, adding a few keywords below so they can find this in future:
evaluate conditional null none empty if node switch node

1 Like