It's all about the answers!

Ask a question

RTC Java API Query for workitems created last 24h

Marcin Blacha (1461214) | asked Dec 01 '22, 3:41 a.m.
edited Dec 01 '22, 4:37 a.m.

I' trying to write server side async task, that will be searching for workitems created in last 24h, that state was not changed. I'm using IQueryCommon and AttributeExpresion:

Date date = new Date();
Timestamp creationDate = new Timestamp(date.getTime()-(24*60*60*1000));
AttributeExpression creationDateExpression = new AttributeExpression(attribute, AttributeOperation.BEFORE, creationDate);


but this expression is not respecting time, only date. In WebUI when you create query, you can select option created in last xx hours:

Is it possible to create this kind of expression in Java API?

2 answers

permanent link
Ralph Schoon (63.3k33646) | answered Dec 01 '22, 11:31 a.m.

The code you show is incomplete. 

In the SDK I find:

assertResult(custom, AttributeOperation.BEFORE, new Timestamp(now.getTime() + ONE_DAY), copy.getWorkItem());

So, I assume you are following the correct approach, but there is something else going on.

the SDK code:

private void assertResult(IAttribute custom, AttributeOperation operator, Object value, IWorkItem expected) throws TeamRepositoryException {
IQueryResult<IResolvedResult<IWorkItem>> result= getResult(custom, operator, value);
assertEquals(1, result.getTotalSize(null));
IWorkItem resultItem=;
assertEquals(expected.getId(), resultItem.getId());

private IQueryResult<IResolvedResult<IWorkItem>> getResult(IAttribute custom, AttributeOperation operator, Object value) throws TeamRepositoryException {
IQueryableAttributeFactory factory= QueryableAttributes.getFactory(IWorkItem.ITEM_TYPE);
IQueryableAttribute attribute= factory.findAttribute(fgProjectArea, custom.getIdentifier(), fgAuditableClient, null);
AttributeExpression expression= new AttributeExpression(attribute, operator, value);
IQueryResult<IResolvedResult<IWorkItem>> result= fgQueryClient.getResolvedExpressionResults(fgProjectArea, expression, IWorkItem.SMALL_PROFILE);
return result;

Ralph Schoon commented Dec 01 '22, 11:38 a.m.

Note in the server you would have to use IAuditableCommon and not the client. 

permanent link
Marcin Blacha (1461214) | answered Dec 02 '22, 4:08 a.m.
edited Dec 02 '22, 4:14 a.m.
Hi Ralph,

here is full function used to find workitems in project area:
private List<IWorkItem> findWorkItems(IProjectAreaHandle projectArea, IProgressMonitor monitor)
            throws TeamRepositoryException {   

        List<IWorkItem> matchingWorkItems = new ArrayList<IWorkItem>();   

        try { 

            IQueryResult<IResolvedResult<IWorkItem>> result;      
        IQueryableAttribute attribute = findQueryableAttribute(projectArea,
                  IWorkItem.CREATION_DATE_PROPERTY, monitor);
            if (attribute == null)
                  return Collections.emptyList();      
            IWorkItemType escalationWItype = findWIType(projectArea, "Escalation", monitor);    

            if (escalationWItype == null)
                  return Collections.emptyList();
            Date now = new Date();

            AttributeExpression creationDateExpression = new AttributeExpression(attribute,
                   AttributeOperation.BEFORE, new Timestamp(now.getTime() - (24*60*60*1000+1000)));  
            IQueryableAttribute projectAreaAttribute = findQueryableAttribute(projectArea,
                   IWorkItem.PROJECT_AREA_PROPERTY, monitor);
            AttributeExpression projectAreaExpression = new AttributeExpression(projectAreaAttribute, 
                   AttributeOperation.EQUALS, projectArea);
            IQueryableAttribute stateAttribute = findQueryableAttribute(projectArea,
                   IWorkItem.STATE_PROPERTY, monitor);
            VariableAttributeExpression stateGroupExpression = new VariableAttributeExpression(
                   stateAttribute, AttributeOperation.EQUALS, 
new StatusVariable(IWorkflowInfo.OPEN_STATES_GROUP));                        IQueryableAttribute typeAttribute = findQueryableAttribute(projectArea, IWorkItem.TYPE_PROPERTY, monitor);                        AttributeExpression wiTypeExpression = new AttributeExpression(typeAttribute, AttributeOperation.EQUALS, escalationWItype.getIdentifier());                      
Term term = new Term(Operator.AND); term.add(projectAreaExpression);
            IQueryCommon queryService = getQueryCommon();             result = queryService
.getResolvedExpressionResults(projectArea, term, IWorkItem.FULL_PROFILE);             while (result.hasNext(monitor)) {                 matchingWorkItems.add(;
            }         }         catch (Exception ex) {             getLog().error(NLS.bind("Error during finding workitems for project area {0}, error message: {1}, stack trace {2}",                     projectArea.toString(), ex.getMessage(), ex.getStackTrace().toString()));\         }         return matchingWorkItems;        }

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.