HTTP A real run on this documentation site's sandbox custom entity
GET /cdna-api/custom-entities/apiSandboxes?externalId=crm-2001&properties={id}
# 200 {"totalCount": 0, "data": []} -> create
POST /cdna-api/custom-entities/apiSandbox?properties={id,externalId,activeRevision{id}}
{"externalId": "crm-2001", "revisions": {"name": "Imported from the CRM", "activeRevision": true, "areas": {"summary": "First import."}}}
# 200 {"id": 7, "externalId": "crm-2001", "activeRevision": {"id": 19}}
GET /cdna-api/custom-entities/apiSandboxes?externalId=crm-2001&properties={id}
# 200 {"totalCount": 1, "data": [{"id": 7}]} -> update
PUT /cdna-api/custom-entities/apiSandbox/7?properties={id,externalId,activeRevision{id}}
{"externalId": "crm-2001", "revisions": {"name": "Imported from the CRM", "activeRevision": true, "areas": {"summary": "Second import, with a change."}}}
# 200 {"id": 7, "activeRevision": {"id": 20}} a change: a new revision
# the same PUT again
# 200 {"id": 7, "activeRevision": {"id": 20}} no change: still revision 20
GET /cdna-api/custom-entities/apiSandbox/7?properties={id,revisions{id}}
# 200 {"id": 7, "revisions": [{"id": 19}, {"id": 20}]}
JavaScript import.js
// import.js: create or update by external id (uses the coredna() helper from the OAuth how-to)
import { coredna } from './coredna.js';
const ONE = 'custom-entities/apiSandbox'; // a record
const MANY = 'custom-entities/apiSandboxes'; // the list
function revisionFrom(source) { // the same source always builds the same revision
return {
name: source.name,
activeRevision: true,
areas: { summary: source.summary ?? null },
};
}
export async function importOne(source) {
const externalId = 'crm-' + source.id;
const found = await coredna('GET', MANY, { query: { externalId, properties: '{id}' } });
if (found.totalCount > 1) throw new Error(externalId + ' is on ' + found.totalCount + ' records');
const body = { externalId, revisions: revisionFrom(source) };
if (found.totalCount === 0) {
const created = await coredna('POST', ONE, { query: { properties: '{id}' }, body });
return ['created', created.data.id];
}
const id = found.data[0].id;
await coredna('PUT', ONE + '/' + id, { query: { properties: '{id}' }, body });
return ['updated', id];
}
// one run at a time, one record after another:
// for (const source of rows) console.log(source.id, ...(await importOne(source)));