Try catch without using java delegate - handle creating duplcate camunda user

Hi All,
My workflow objective is:

  1. Check if the user with a specific username exist
  2. If not create one
    Very simple :slight_smile:

I did not find any non-deprecated method of checking if any user exist: IdentityService (camunda BPM Javadocs 7.3.7-ee)

So my current approach is to just handle the error which is thrown when creating a duplicate user.
I used try-catch but it’s not handling in a script of type javascript.
Code I used is:

throw new org.camunda.bpm.engine.delegate.BpmnError(“jdbcUpdateException”);

can anyone guide me here?
Thanks a lot

Hi @teja_polisetty
I created something like this for an example on how to create users and tenets. You can find the full code snippet here. The important stuff looks like this:

  final boolean alreadyCreated = this.doesUserAlreadyExist(user.getEmail(), execution);
        if (alreadyCreated) {
            throw new BpmnError("AlreadyExists");

And the method being used there looks like this, instead of email you can use some other identifier i guess.

    private boolean doesUserAlreadyExist(final String email, final DelegateExecution execution) {
        final User user = (User)execution.getProcessEngineServices().getIdentityService().createUserQuery().userEmail(email).singleResult();
        return user != null;

Big thanks @Niall
I want to ask one more silly question,
In java we are importing with this statement: import org.camunda.bpm.engine.identity.User;,
How can I import the same in service task script of type javascript in the BPMN.

Because without importing if i do, objective is acieved.
But if want to store it in camunda variable and get it again, that i am unable to do here

As you can see in image, user details are stored. But in deserialized format.
Can you provide steps of how i can read the value from this variable