The chat responses are generated using Generative AI technology for intuitive search and may not be entirely accurate. They are not intended as professional advice. For full details, including our use rights, privacy practices and potential export control restrictions, please refer to our Generative AI Service Privacy Information. As this is a test version, please let us know if something irritating comes up. Like you get recommended a chocolate fudge ice cream instead of an energy managing application. If that occurs, please use the feedback button in our contact form!
Skip to content

Patterns

2612

This page collects the shapes that endpoint-backed applications take in practice. Each pattern names the problem, sketches the script and says what the view does with the result.

1. Aggregation: one request instead of many

Problem. The view needs data from many objects: the link tree of a work item, all test cases of a requirement with their last result, the work items of a plan with their time records.

Script. Walk the objects on the server and return a compact structure shaped for the UI.

const trackerService = context.scriptContext().services().tracker();
let result = {};
try {
  const [projectId, wiId] = arguments.getAsString('objectId').split('/');
  const wi = trackerService.getWorkItem(projectId, wiId);
  const links = wi.getLinkedWorkItemsStructsDirect().toArray();
  const children = [];
  for (let i = 0; i < links.length; i++) {
    const target = links[i].getLinkedItem();
    children.push({ id: target.getId(), title: target.getTitle(), role: links[i].getLinkRole().getName() });
  }
  result = { id: wi.getId(), children: children };
} catch (e) {
  result = { error: 'aggregate failed: ' + e };
}
result;

View. One endpointFetch, then render. See Traceability with endpoints.

2. Validation and rules

Problem. Check a set of work items against rules that need several fields, links or history: "every requirement has a test", "no open item older than 90 days without an assignee".

Script. Accept a list of ids (POST), evaluate the rules, return findings per item plus a summary. Keep rule code in small functions so that adding a rule is one function and one entry in a list.

const rules = [
  { code: 'no-description', check: (wi) => !wi.getDescription() || wi.getDescription().getContent().trim() === '' },
  { code: 'unassigned', check: (wi) => wi.getAssignees().isEmpty() },
];

View. A sidebar that validates the selection as it changes and offers fixes. See Release checklist.

3. Bulk writes with a clear outcome

Problem. Apply the same change to many items: assign, set a field, add a link, transition the workflow.

Script. POST with the ids (as a JSON string, plus the mandatory objectId) and the change; loop, save() each item, collect successes and failures instead of aborting on the first error.

const updated = [], failed = [];
for (const fullId of ids) {
  try {
    const [p, id] = fullId.split('/');
    const wi = trackerService.getWorkItem(p, id);
    wi.addAssignee(user);
    wi.save();
    updated.push(fullId);
  } catch (e) {
    failed.push({ id: fullId, error: String(e) });
  }
}
result = { updated: updated, failed: failed };

View. Report both lists; re-run the read afterwards. The REST API can also do bulk updates, but an endpoint gives one round trip and a single, tailored result.

4. External systems with server-side secrets

Problem. Notify a chat channel, create a ticket in another tool, fetch data from an internal service that must not be reachable from the browser.

Script. Read the secret from the user account vault, call the system with context.scriptContext().utils().httpClient(), return a summary.

const httpClient = context.scriptContext().utils().httpClient();
const HttpRequest = Java.type('java.net.http.HttpRequest');
const BodyPublishers = Java.type('java.net.http.HttpRequest$BodyPublishers');
const URI = Java.type('java.net.URI');
// const webhook = <read from the vault>;
const request = HttpRequest.newBuilder(URI.create(webhook))
  .header('Content-Type', 'application/json')
  .POST(BodyPublishers.ofString(JSON.stringify(card)))
  .build();
const response = httpClient.sendRequest(request);

View. A button and a status line. The reference application tsapp-1 contains a complete Teams notification script.

5. Computed data for dashboards

Problem. A navigation topic needs counts, trends or groupings over the whole project.

Script. Query with the tracker service, aggregate in a loop, return the numbers. Add parameters for the query and the grouping so that one endpoint serves several widgets.

View. Render tiles and charts; refresh on demand. Combine with the REST API for the detail table below the charts.

6. Configuration without an administration view

Problem. The application needs settings (a mapping table, thresholds, a target URL) until administration views arrive.

Script. A GET endpoint reads the settings from a known place: a document in a configuration space, a work item of a dedicated type, or a JSON file in the global configuration repository. A POST endpoint writes them, checking that the caller is an administrator through the security service.

View. A small settings form in a navigation topic, visible only to administrators.

7. Caching expensive results

Endpoints hold no state between calls. For results that are expensive and change rarely:

  • cache in the view for the lifetime of the page (useMemo, a module-level map keyed by parameters);
  • let the browser cache GET responses by making the URL stable (same parameters, no timestamp);
  • for shared, long-lived caches, store the computed data in Polarion (a document or attachment) with a time stamp and let the endpoint return it when fresh enough.

Anti-patterns

  • A single "do everything" endpoint with a command parameter. Separate endpoints are easier to secure, test and document.
  • Returning Java objects or deeply nested raw structures. Map to the fields the UI needs.
  • Writes in GET endpoints.
  • Trusting input. Validate ids, lengths and enumerations before touching data.
  • Long-running loops without limits. Cap the depth or page the work; the caller waits for the whole script. See Performance and load.