How to catch exception invalid user exception in version 24.4 ?

Certified Associate Developer

in version 24.4 isuseractive function is not available, is there any other way to achieve this? I just want to validate if user is acitve or deactivated or even if doesn't exist anymore in the system, we are trying to validate the scenario if for any reason the user saved in DB was deactived or deleted from the system, cause currently we are saving the result of calling the funcion loggedInUser() in DB, this is storing a text value, however we have the requirement to display the fisrtName and lastName of the user in the frontend, but if the user doesn't exist in the DB we are getting  invalid user exception at the moment to use user(username,property) function, since we don't have a try-catch similiar function in version 24.4, I would like to know if there is any other way to catch the exception or change the approach following your recomendations. Any help will be greatly appreciated, thanks and regards!

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer
    is there any other way to achieve this?

    Copying my answer from your other post in the zombie thread, in case you didn't see it:

    With the current state of the User Record, I've found that the best (and arguably fastest) username-checking routine is now to use a custom-built expression rule that queries said Record type.  Using the custom query I designed, this is also the only solution I've found that can treat the username as case-insensitive enough to work without laborious other bending-over-backwards workarounds (like you need to use with the "isUsernameTaken()" and "isusernameavailablefornewaccount()" type rules).

    a!localVariables(
      local!username: a!defaultValue(trim(ri!username), "-=-=-"),
      
      local!userQuery: a!refreshVariable(
        refreshAlways: true(),
        value: if(
          local!username = "-=-=-",
          a!map(totalCount: 0),
          a!queryRecordType(
            recordType: 'recordType!{SYSTEM_RECORD_TYPE_USER}User',
            pagingInfo: a!pagingInfo(1, 1),
            fetchTotalCount: true(),
            fields: {'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_username}username', 'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_active}active', 'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_uuid}uuid'},
            filters: a!queryLogicalExpression(
              operator: "OR",
              filters: {
                /* doing this to rule out lowercase username first, as it's the normal way we try to initialize them */
                a!queryFilter(
                  field: 'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_username}username',
                  operator: "=",
                  value: lower(local!username)
                ),
                a!queryFilter(
                  field: 'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_username}username',
                  operator: "=",
                  value: local!username
                )
              },
              logicalExpressions: {
                a!queryLogicalExpression(
                  operator: "AND",
          
                  /* these will require the username to match exactly except for casing */
                  filters: {
                    a!queryFilter(
                      field: 'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_username}username',
                      operator: "starts with",
                      value: local!username
                    ),
                    a!queryFilter(
                      field: 'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_username}username',
                      operator: "ends with",
                      value: local!username
                    )
                  }
                )
              }
            )
          )
        )
      ),
      
      /* tool written by: Mike Schmitt */
      
      if(
        local!userQuery.totalCount = 0,
        a!map(
          exists: false(),
          username: if(local!username = "-=-=-", "(none provided)", local!username)
        ),
        a!map(
          exists: true(),
          username: tostring(local!userQuery.data['recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_username}username']),
          uuid: local!userQuery.data[1]['recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_uuid}uuid'],
          isActive: local!userQuery.data[1]['recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_active}active']
        )
      )
    )

    invalid username:

    active (test) user:

    inactive user:

    same user but with mismatched casing:

Reply
  • 0
    Certified Lead Developer
    is there any other way to achieve this?

    Copying my answer from your other post in the zombie thread, in case you didn't see it:

    With the current state of the User Record, I've found that the best (and arguably fastest) username-checking routine is now to use a custom-built expression rule that queries said Record type.  Using the custom query I designed, this is also the only solution I've found that can treat the username as case-insensitive enough to work without laborious other bending-over-backwards workarounds (like you need to use with the "isUsernameTaken()" and "isusernameavailablefornewaccount()" type rules).

    a!localVariables(
      local!username: a!defaultValue(trim(ri!username), "-=-=-"),
      
      local!userQuery: a!refreshVariable(
        refreshAlways: true(),
        value: if(
          local!username = "-=-=-",
          a!map(totalCount: 0),
          a!queryRecordType(
            recordType: 'recordType!{SYSTEM_RECORD_TYPE_USER}User',
            pagingInfo: a!pagingInfo(1, 1),
            fetchTotalCount: true(),
            fields: {'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_username}username', 'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_active}active', 'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_uuid}uuid'},
            filters: a!queryLogicalExpression(
              operator: "OR",
              filters: {
                /* doing this to rule out lowercase username first, as it's the normal way we try to initialize them */
                a!queryFilter(
                  field: 'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_username}username',
                  operator: "=",
                  value: lower(local!username)
                ),
                a!queryFilter(
                  field: 'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_username}username',
                  operator: "=",
                  value: local!username
                )
              },
              logicalExpressions: {
                a!queryLogicalExpression(
                  operator: "AND",
          
                  /* these will require the username to match exactly except for casing */
                  filters: {
                    a!queryFilter(
                      field: 'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_username}username',
                      operator: "starts with",
                      value: local!username
                    ),
                    a!queryFilter(
                      field: 'recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_username}username',
                      operator: "ends with",
                      value: local!username
                    )
                  }
                )
              }
            )
          )
        )
      ),
      
      /* tool written by: Mike Schmitt */
      
      if(
        local!userQuery.totalCount = 0,
        a!map(
          exists: false(),
          username: if(local!username = "-=-=-", "(none provided)", local!username)
        ),
        a!map(
          exists: true(),
          username: tostring(local!userQuery.data['recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_username}username']),
          uuid: local!userQuery.data[1]['recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_uuid}uuid'],
          isActive: local!userQuery.data[1]['recordType!{SYSTEM_RECORD_TYPE_USER}User.fields.{SYSTEM_RECORD_TYPE_USER_FIELD_active}active']
        )
      )
    )

    invalid username:

    active (test) user:

    inactive user:

    same user but with mismatched casing:

Children
No Data