How to Assign Last Label Name to Variable or "null" if Empty in UiPath

Hello UiPath Community,

I am working on a project involving email communication analysis, where I extract labels from a stream of emails, process them, and queue different data types. I need to assign the last label’s name from each email to a variable Category. However, if no labels are present, Category should be assigned “null”.

Here is the expression I’m using for assignment:
If(in_Listofcategory(0).name.Last().ToString IsNot Nothing, in_Listofcategory(0).name.Last().ToString, “null”)

Issues Encountered:

  1. The assignment does not work correctly; Category sometimes does not receive any value, especially when the labels are missing from the emails.
  2. The process should assign “null” to Category when no labels data is present and the last label’s name when it is present.

Question:

Can anyone suggest how to modify the expression to reliably assign “null” when no labels are present and the last label’s name otherwise? I suspect the issue might be with how I’m handling potentially empty or Nothing values.

Additional Context:

  • The labels are dynamically extracted from each email.
  • The in_Listofcategory is a list populated with these labels.

Any suggestions or corrections to my approach would be greatly appreciated!

we would recommend to check the LastOrDefault() Linq Operator

we have doubts on the part
in_Listofcategory(0)

but could rewrite to (without IF)
in_Listofcategory(0).Name.Select(Function (x) x.ToString()).LastOrDefault("null")

@samantha_shah

did you try Enumerable.LastOrDefault(in_Listofcategory(0).name,"Null")this will assign blank

Also if you feel if in_ListCategory can also be empty then use below

If(in_ListCategory.Count>0,Enumerable.LastOrDefault(in_Listofcategory(0).name,"Null"),"Null")

cheers

cheers

Hi @ppr

the in_Listofcategory is list of type Result(stream.models)
therfore Select is not a member

Hi @Anil_G

thanks for the reply

i amtrying i use this query like this because in_ListCategory cannot be indexed only Labels can be indexed
If(in_Listcategory.Count>0,Enumerable.LastOrDefault(in_Listcategory…Labels(0).name,“Null”),“Null”)

and count is not a member of results

Hi @samantha_shah

Give a try with

If(in_Listofcategory IsNot Nothing AndAlso in_Listofcategory.Count > 0 AndAlso in_Listofcategory(0).name.Count > 0, in_Listofcategory(0).name.LastOrDefault(), "Null")

Regards!

@samantha_shah

Can you tell the variable type please …accordingly we cna suggest

Cheers

will create more doubts on where Last operator is provided. We hope that is not

  • the offered Last Operator from Name and Name is a singular string

In general we would expect that Name is a String Array

Hi Everyone actually the results worked for me by using this query:
given by @Anil_G

i slightly modifed it to iterate inside the labels index and it worked for me , thankyou for your help

If(in_Listofcategory.Labels.Count>0,Enumerable.LastOrDefault(in_Listofcategory.Labels(0).name,“Null”),“Null”)

Thankyou for your reply @fernando_zuluaga

it worked from the prvious users idea.