How to get the User id of a referenced element which has been added as a model element?

Hi guys
I want to get the User id of a referenced element which has been added as a model element
For example, I have a Task element which has a reference to a use-case element
Using the below code I can get the model element of the referenced element but when I try to get its view it returns a null value.

IReference reference;
IModelElement model = reference.getUrlAsModel();
IDiagramElement view = model.getMasterView();
IUseCase usecase = (IUseCase) view ;
String userId = usecase.getUserID();

This code will not work and it causes a null pointer exception on the fourth line because the view object is null. I have used the getMasterView() method before and it worked fine but in this case and for a reference that has been added as a model element it not work and returns null.

Would you please help me find the user id of that use-case element?
Thanks in advance

Dear Mamad,

We are not very certain about what you mean by task here. Do you mean BPMN task? And what sort of “reference” do you mean? Model element reference? Could you post some screenshots for referencing?

Best regards,
Jick Yeung

Yeah I mean BPMN task and the reference has been added as a model element reference

For your sure here is an example of what I mean :

This is the BPMN task

And this one is the use case element

I want to get the user id of that use case element but my code does not work.

Please also note that the task element and the use case element may be in two different teamwork projects.

Thanks

Hi Mamad,

I am asking our engineers. Please be patient and I will get back to you with an answer as soon as possible.

Best regards,
Jick Yeung

Very well, thank you

Hi Mamad,

Please try:

IModelProperty userIdProp = referencedElement.getModelPropertyByName(IUseCase.PROP_USER_ID);
if (userIdProp != null) {
userIdProp.getValueAsString() // userId returned
}

Best regards,
Jick Yeung

1 Like

Hi @Jick and thanks for your reply
I tested your code for both references as model element and references as shape but userIdProp is null every time.
I tested that using this code :

IModelElement modelElement; // assume i have access to model element of the task
IBPTask task = (IBPTask) modelElement;
for(IReference reference : task.toReferenceArray()) {
    if(reference.getUrlAsModel() != null || reference.getUrlAsShape() != null ) {
        IModelProperty userIdProp = reference.getModelPropertyByName(IUseCase.PROP_USER_ID);
        if (userIdProp != null) { 
            return userIdProp.getValueAsString();
        } else {
            return null;
        } 
    } 
} 

and it will return null all the time

:roll_eyes:
Any idea?

Hi Mamad,

I have forwarded your question to our team. When there is a reply, I will let you know.

Best regards,
Jick Yeung

Hi Mamad,

IReference is NOT referencedElement. Please retrieve the referenced element from IReference as said here:

Best regards,
Jick Yeung

1 Like

Yes you are right
Thank you so much for the support :pray: