It's all about the answers!

Ask a question

RTC Server API code to find the result of a query

Vivek Bandhu (636) | asked Sep 14 '17, 5:53 a.m.

 I have to develop a plugin on RTC which will run when a query on a project area and store the result in a customized attributes. 

for example -  query as - 

Major Defect = Number of Defects wich respect those conditions for the currunt milestone : <o:p> </o:p>

·         Subtype :  external AND <o:p> </o:p>

·         Severity : Major AND <o:p> </o:p>

·         Status IS NOT Info Requested or Reject AND <o:p> </o:p>

Query Name : Open Defect – Result : 42 defects. Get the “42” and put it on a custom attribute .

How to do it .

3 answers

permanent link
Ralph Schoon (63.1k33646) | answered Sep 14 '17, 7:31 a.m.

You could try to start with:

Note that the api is client API. You will have to try to find common API in the server. E.g.

instead of


I haven'd tried thisin the server API, so that is all I have.

permanent link
Luca Martinucci (1.0k294112) | answered Sep 22 '17, 3:50 a.m.

I have done similiar things several times.
This is a code snippet to build and execute a query on work items:

IQueryableAttributeFactory queryAttrFactory = QueryableAttributes.getFactory(IWorkItem.ITEM_TYPE);
IQueryableAttribute projectAreaAttribute = queryAttrFactory.findAttribute(projectAreaHandle,IWorkItem.PROJECT_AREA_PROPERTY, utility.workItemCommon.getAuditableCommon(), utility.monitor);
AttributeExpression projectAreaExpression = new AttributeExpression(projectAreaAttribute, AttributeOperation.EQUALS, projectAreaHandle);
IQueryableAttribute typeAttribute = queryAttrFactory.findAttribute(projectAreaHandle,IWorkItem.TYPE_PROPERTY, utility.workItemCommon.getAuditableCommon(), utility.monitor);
AttributeExpression typeExpression = new AttributeExpression(typeAttribute, AttributeOperation.EQUALS, "allowedreference");
IQueryableAttribute referencingWIAttribute = queryAttrFactory.findAttribute(projectAreaHandle, referencingWorkItemTypeAttribute, utility.workItemCommon.getAuditableCommon(), utility.monitor);
AttributeExpression referencingWIExpression = new AttributeExpression(referencingWIAttribute, AttributeOperation.EQUALS, referencingWI);
IQueryCommon queryService = getService(IQueryCommon.class);
Term term = new Term(Term.Operator.AND);
IQueryResult<IResolvedResult<IWorkItem>> queryResult = queryService.getResolvedExpressionResults(projectAreaHandle, term, IWorkItem.FULL_PROFILE);

Then you loop over the result set:

int resultSize = queryResult.getResultSize(utility.monitor).getTotal();
if (resultSize>0) {    
    while (queryResult.hasNext(utility.monitor)) {
    IResolvedResult<IWorkItem> resolvedResultWI =;
    IWorkItem resolvedWI = resolvedResultWI.getItem();
    // .....
I hope it helps.

Stefano Antoniazzi commented Sep 28 '17, 10:37 a.m.

 Hi Luca, even if your code really makes sense, I would keep the main loop about queryResult.hasNext and for every page I would check queryResult.getResultSize to see if there are results on that page.

permanent link
Emiliano Iannetti (114) | answered May 07 '20, 7:20 a.m.

 Hi, thanks and sorry for a question, I try to execute your code, as well as that of Ralph that I have seen on other posts, but unfortunately I always get this error:

Exception checking precondition. An unhandled exception occurred during "Prohibit Save (ASISE)".
Argument must be an instance of IAuditableHandle.
Now do not notice you have details but it is a CustomAdvisor that gives the problem as if I had not instantiated something correctly but what ?? Thanks.

Ralph Schoon commented May 11 '20, 3:25 a.m.

The classes used in the API, that represent objects, can be either basic stuff such as String, or it can be more complex stuff. Usually these are represented by a handle e.g. IWorkitemHandle. Things that get traced in history are usually implementing IAuditableHandle. So somewhere in your code you pass a wrong class/interface. 

Emiliano Iannetti commented May 11 '20, 3:28 a.m. | edited May 11 '20, 3:50 a.m.

 Good morning, and I imagined it was something similar, in fact I used the plannedFor as a parameter convinced that it was right, but using a string that replicates the value of the PlannedFor I managed to run the query. Thanks.

Ralph Schoon commented May 11 '20, 3:51 a.m.
Good it is working for you now. 

Please ask your own question next time to not pollute existing content. Also please understand the difference between a comment and an answer. 

Your answer

Register or to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.