---
title: Compare two versions
summary: Compare a version with the one before it from code, read the categories, the similar changes and a shared move, follow objects to the new version, and turn an upload's changes into BUL.
order: 54
since: 3.0
languages: [javascript]
terms: [bcol, bul, qid, version]
see-also: [reference/bcol#api, explanation/comparing-versions, how-to/using-bimworks/compare-versions, reference/bul]
---

# Compare two versions

For an integration that reports what changed in a new upload, re-points its own references to the objects of the new version, or hands the changes on. A compare takes two [[term:version|versions]] and a [[term:bcol|BCOL]] definition and returns every object in one category; the request and result are described in full on [[page:reference/bcol#api]], and the app does the same in its compare panel ([[page:how-to/using-bimworks/compare-versions]]).

## Before you start

- A login with the `query:run` scope (and `model:write` to record an upload as a transaction) and read rights on both models. The calls do not check the [[feature:BCOL]] feature, which switches on the compare editor in the app.
- The version UUIDs ([[page:how-to/models/list-the-tree]]).

## 1. Compare

```javascript {#compare-api}
const previous = await client.fs.previousVersion(latest);
const result = await client.ifc.compareModels({
	left: previous.uuid,
	right: latest.uuid,
	bcol: "compare (attribute.Name, geometry);"
});
for (const category of result.categories) {
	console.log(category.label, category.count);
}
// the objects of a category as a QidSet, and the object a pair pairs
const removed = result.objects("top.removed", "left");
const before = result.counterpart(latest.uuid + "-43");
// changes that are alike, and a move most objects share
for (const cluster of result.clusters) {
	console.log(cluster.label, result.clusterObjects(cluster.id).size);
}
if (result.alignment) {
	console.log("everything moved by", result.alignment.translation);
}
```

Over HTTP it is [[openapi:CompareModels]], and [[openapi:GetCompareResult]] reads a stored result again, page by page. A `bcol` can be BCOL text, the name of a built-in definition or a stored Compare action, or the definition as JSON.

## 2. What a version changed, and where objects went

```javascript {#version-changes}
// what the latest upload changed, and where stored objects went
const [changes] = await client.ifc.versionChanges(latest.uuid, {onlyStored: true});
const mapped = await client.ifc.mapQids({from: previous.uuid, to: latest.uuid, oids: [43, 44]});
```

[[openapi:GetVersionChanges]] gives the counts of the default compare with the version before, which the server computes on upload; [[openapi:MapQids]] maps objects of one version to their counterparts in another, for re-pointing issues, notes or saved selections. [[openapi:CompareObjects]] compares any two objects as one pair, and [[openapi:ExportCompareResult]] downloads a stored result as Excel, CSV, JSON or BCF.

## 3. The changes as BUL

```javascript {#compare-bul}
// the upload's changes as BUL, and the upload replaced by a transaction with them
const bul = await client.ifc.getCompareBul(previous.uuid, latest.uuid);
console.log(bul.coverage.in_bul + " of " + bul.coverage.changes + " changes", bul.bul);
if (bul.record.possible) {
	await client.ifc.recordUploadAsTransaction(latest.uuid);
}
```

[[openapi:GetCompareBul]] writes what changed as [[term:bul|BUL]] statements on the older version and says what BUL cannot write; [[openapi:RecordUploadAsTransaction]] replaces the upload by a transaction with those changes when all of them can be written ([[page:explanation/comparing-versions#from-a-compare-back-to-an-edit]]). The pairs fixed by hand are [[openapi:SetComparePairOverride]], [[openapi:ListComparePairOverrides]] and [[openapi:DeleteComparePairOverride]], with [[openapi:GetMatchCandidates]] for the candidates.

## Check

Compare the latest version of a model with the one before it: the counts of `result.categories` are the ones the model row in the app shows on its change badge, and `versionChanges` returns them too.
