Check if record exists in database

Hello. I have a workflow which requires to check if an email exists in a database entity. If it does, then it should perform some activities. Likewise, if it doesn’t, then it should perform another set of activities. After querying the data, I saved the Output result to a variable called ResultEntity. In the ElseIf activity, I set the following:

IF
ResultEntity(0).EmailAddress.ToString = EmailAddress(Variable)

ELSE IF
ResultEntity(0).EmailAddress.ToString <> EmailAddress(Variable)

Upon debugging, if the email indeed exists, then no error shows. However, if it doesn’t retrieve an email, then I get an error that index was out range. So how do I make the error disappear? There is no continue on error option.

  1. Query the data and save the result to the variable ResultEntity.
  2. Check if ResultEntity has any records.
  3. If ResultEntity has records (Count > 0), perform your existing IF-ELSE logic:
    • IF ResultEntity(0).EmailAddress.ToString = EmailAddress(Variable), then do Activity A.
    • ELSE IF ResultEntity(0).EmailAddress.ToString <> EmailAddress(Variable), then do Activity B.
  4. If ResultEntity has no records (Count = 0), this means the email does not exist in the database. Perform Activity C (for the scenario when the email is not found).

Hi @private_matter

Assign activity:
IsEmailFound = (ResultEntity.Count > 0)

IF activity:
Condition: IsEmailFound = True
Body:
’ Email exists in the entity, perform activities for existing email.
’ For example, you can access the email using ResultEntity(0).EmailAddress.ToString

ELSE IF activity:
Condition: IsEmailFound = False
Body:
’ Email does not exist in the entity, perform activities for non-existing email.
’ For example, you can handle the scenario where the email is not found.

I hope it helps!!

Hi @private_matter ,

An initial Check prepended to the original check done should help avoid errors :

IF
ResultEntity.Any AndAlso ResultEntity(0).EmailAddress.ToString = EmailAddress(Variable)

ELSE IF
ResultEntity.Any AndAlso ResultEntity(0).EmailAddress.ToString <> EmailAddress(Variable)

Hi @private_matter

  1. Check if ResultEntity is not null and has at least one element before accessing the email address.
  2. Use the ResultEntity’s Count property to determine the number of elements in the collection. If the count is greater than zero, then there is at least one email address in the ResultEntity.’
IF
ResultEntity IsNot Nothing AndAlso ResultEntity.Count > 0

THEN
Assign: ExistingEmailFound = True

ELSE
Assign: ExistingEmailFound = False

Hope it helps!!
Regards,

Done thx all @lrtetala @supermanPunch @Parvathy @pravallikapaluri
Learned something new today.

1 Like

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