# How to call @QueryMethod from another Workflow

**URL:** <https://community.temporal.io/t/how-to-call-querymethod-from-another-workflow/5217>\
**Category:** Community Support\
**Tags:** java-sdk\
**Created:** [July 5, 2022, 9:13pm UTC](https://community.temporal.io/t/how-to-call-querymethod-from-another-workflow/5217 "2022-07-05T21:13:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![korad](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/korad/32/2361_2.png) [@korad](https://community.temporal.io/u/korad)\
**Post date:** [July 5, 2022, 9:13pm UTC](https://community.temporal.io/t/how-to-call-querymethod-from-another-workflow/5217/1 "2022-07-05T21:13:33Z")

</div>

Hi  
I’m looking a way to call @QueryMethod of one workflow from another. I’ve note from here [Cannot call @QueryMethod from child workflow](https://community.temporal.io/t/cannot-call-querymethod-from-child-workflow/1247/2) that I need to querying from Activity and use WorkflowClient to get workflowStub.

```auto
  public String someActivityMethod(String workflowId, String queryType) {
    WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
    WorkflowClient client = WorkflowClient.newInstance(service);
    WorkflowStub workflow = client.newUntypedWorkflowStub(workflowId);
    return workflow.query(queryType, String.class);
  }

```

But in this case I’m getting error

```auto
io.grpc.StatusRuntimeException: UNAVAILABLE: io exception 
io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:262)
io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:243)
io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:156)
io.grpc.health.v1.HealthGrpc$HealthBlockingStub.check(HealthGrpc.java:252)
io.temporal.serviceclient.WorkflowServiceStubsImpl.lambda$checkHealth$2(WorkflowServiceStubsImpl.java:273)
io.temporal.internal.retryer.GrpcSyncRetryer.retry(GrpcSyncRetryer.java:61)
io.temporal.internal.retryer.GrpcRetryer.retryWithResult(GrpcRetryer.java:51)
io.temporal.serviceclient.WorkflowServiceStubsImpl.checkHealth(WorkflowServiceStubsImpl.java:266)
io.temporal.serviceclient.WorkflowServiceStubsImpl.<init>(WorkflowServiceStubsImpl.java:173)
io.temporal.serviceclient.WorkflowServiceStubs.newInstance(WorkflowServiceStubs.java:51)
io.temporal.serviceclient.WorkflowServiceStubs.newInstance(WorkflowServiceStubs.java:41)
io.temporal.serviceclient.WorkflowServiceStubs.newInstance(WorkflowServiceStubs.java:36)

```

Help me please, how can i get workflowStub in activity or sugest another way to call @QueryMethod from workflow.

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [July 5, 2022, 9:57pm UTC](https://community.temporal.io/t/how-to-call-querymethod-from-another-workflow/5217/3 "2022-07-05T21:57:12Z")

</div>

Hi @korad

Regarding the “Unavailable” error message, are you running Temporal server locally? Can you show how you create WorkflowServiceStubs and WorkflowClient in your client code that starts workflow exec?

You don’t have to create client in your activity methods (those are typically heavy-weight operations), you can pass it to your activity instance when you register it with the worker, for example:

` worker.registerActivitiesImplementations(new MyActivitiesImpl(client));`

(add a constructor to your activities impl that takes in WorkflowClient as input)

Yes, you cannot query using ExternalWorkflowStub (just signal) so you would need to use activity for it.

In your activity code I think you should also catch WorkflowException and then check its type: `WorkflowNotFoundException` in case workflowId is invalid, and `WorkflowQueryException` in case the queryType passed in is incorrect, if you need to handle these cases gracefully.

---

<div class="post-metadata">

**Author:** ![korad](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/korad/32/2361_2.png) [@korad](https://community.temporal.io/u/korad)\
**Post date:** [July 6, 2022, 1:43pm UTC](https://community.temporal.io/t/how-to-call-querymethod-from-another-workflow/5217/4 "2022-07-06T13:43:30Z")

</div>

Thanks @tihomir, your answer was helpful!  
I use spring to manage Activities and WorkflowClient beans, so it was enough for me to autowire WorkflowClient to Activity and use it there.
