HTTP Real answers from this documentation site's sandbox menu element
PUT /cdna-api/menus/element/53?properties={id,customFieldContent}
Content-Type: application/json
{"customFields": {"apiTestText": "Written by name"}}
# 200: apiTestText changed, apiTestNote and apiTestImage kept their values
{"customFields": {"apiTestTxt": "typo"}}
# 422 {"validationErrors": {"customFields.apiTestTxt": [{"message": "No custom field found for Menus\\Entities\\Element named apiTestTxt"}]}}
{"customFields": {"apiTestImage": 999999}}
# 422 {"validationErrors": {"image": [{"message": "Could not find the referenced record with id 999999"}]}}
JavaScript A product sync; the field names are this example's
// sync-fields.js: write only what changed (uses the coredna() helper from the OAuth how-to)
import { coredna } from './coredna.js';
// the other system's attribute -> the site's field, and how to convert the value
const FIELDS = {
material: { name: 'material', convert: v => v.toLowerCase() }, // a select: its option key
certified: { name: 'certified', convert: v => v === 'Y' }, // a switch
careSheet: { name: 'careSheet', convert: v => v ? fileIds[v] : null }, // a file: its id, uploaded earlier
};
const fileIds = {}; // filled by the upload step: source file name -> id
function current(area) { // a customFieldContent entry -> the value a write takes
if (!area) return null;
if (area.category === 'image') return area.imageId ?? null;
if (area.category === 'file') return area.fileId ?? null;
if ('active' in area) return area.active; // a switch
return area.text ?? null;
}
export async function syncProduct(productId, source) {
const { data } = await coredna('GET', 'products/product/' + productId, {
query: { properties: '{id,customFieldContent}' },
});
const stored = data.customFieldContent ?? {};
const changes = {};
for (const [attribute, { name, convert }] of Object.entries(FIELDS)) {
if (!(attribute in source)) continue;
const value = convert(source[attribute]);
if (String(current(stored[name]) ?? '') !== String(value ?? '')) changes[name] = value;
}
if (Object.keys(changes).length === 0) return 'unchanged';
await coredna('PUT', 'products/product/' + productId, {
query: { properties: '{id}' },
body: { customFields: changes },
});
return Object.keys(changes);
}