{"record":{"id":"5a750dc123a37949","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-customfield-json","errorCode":"error-invalid-customfield-json","errorMessage":"Invalid JSON for Custom Fields","messagePattern":"Invalid JSON for Custom Fields","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/users/saveCustomFieldsWithoutValidation.ts","lineNumber":16,"sourceCode":"import type { IUser } from '@rocket.chat/core-typings';\nimport type { Updater } from '@rocket.chat/models';\nimport { Subscriptions, Users } from '@rocket.chat/models';\nimport { Meteor } from 'meteor/meteor';\nimport type { ClientSession } from 'mongodb';\n\nimport { trim } from '../../../lib/utils/stringUtils';\nimport { onceTransactionCommitedSuccessfully } from '../../database/utils';\nimport { settings } from '../../settings';\nimport { notifyOnSubscriptionChangedByUserIdAndRoomType } from '../notifyListener';\n\nconst getCustomFieldsMeta = function (customFieldsMeta: string) {\n\ttry {\n\t\treturn JSON.parse(customFieldsMeta);\n\t} catch (e) {\n\t\tthrow new Meteor.Error('error-invalid-customfield-json', 'Invalid JSON for Custom Fields');\n\t}\n};\nexport const saveCustomFieldsWithoutValidation = async function (\n\tuserId: string,\n\tformData: Record<string, any>,\n\toptions?: {\n\t\t_updater?: Updater<IUser>;\n\t\tsession?: ClientSession;\n\t},\n): Promise<void> {\n\tconst customFieldsSetting = settings.get<string>('Accounts_CustomFields');\n\tif (!customFieldsSetting || trim(customFieldsSetting).length === 0) {\n\t\treturn;\n\t}\n\n\t// configured custom fields in setting\n\tconst customFieldsMeta = getCustomFieldsMeta(customFieldsSetting);\n","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/users/saveCustomFieldsWithoutValidation.ts#L1-L34","documentation":"saveCustomFieldsWithoutValidation throws error-invalid-customfield-json when JSON.parse of the Accounts_CustomFields setting string fails inside getCustomFieldsMeta. The admin-managed setting that declares custom profile fields is not valid JSON, so every profile save that includes custom fields dies before touching the database.","triggerScenarios":"Any saveCustomFields call (profile save with custom fields, user creation with custom fields) after an admin saved a malformed Accounts_CustomFields value: trailing commas, single quotes, unquoted keys, comments, or smart quotes pasted from a word processor.","commonSituations":"Hand-editing the custom-fields definition in the admin UI; copy-pasting examples from docs/blogs that contain comments or YAML-style syntax; an integration writing the setting programmatically without JSON.stringify.","solutions":["Open Administration > Settings > Accounts > Custom Fields and fix the JSON (validate it with JSON.parse or a JSON linter first); an empty value is also acceptable","When writing the setting programmatically, always build it with JSON.stringify instead of string concatenation","Add a pre-save validation of the setting so a bad value can never be persisted"],"exampleFix":"// before\nSettings.update('Accounts_CustomFields', rawConfigText); // may persist broken JSON\n\n// after\nJSON.parse(rawConfigText); // throws before saving if invalid\nSettings.update('Accounts_CustomFields', rawConfigText);","handlingStrategy":"validation","validationCode":"// validate the admin setting before it can poison every profile save\nconst parseCustomFields = (raw: string): Record<string, unknown> | null => {\n  try {\n    return JSON.parse(raw);\n  } catch {\n    return null; // reject the value before persisting it\n  }\n};","typeGuard":"const isValidCustomFieldsSetting = (raw: string): boolean => {\n  const t = raw.trim();\n  return t.length === 0 || parseCustomFields(t) !== null;\n};","tryCatchPattern":"try {\n  await saveCustomFieldsWithoutValidation(uid, formData);\n} catch (e) {\n  if (isMeteorErrorCode(e, 'error-invalid-customfield-json')) {\n    // workspace-level misconfiguration: fix Accounts_CustomFields, not this request\n    notifyAdmins('Accounts_CustomFields contains invalid JSON');\n  }\n}","preventionTips":["Validate custom-fields JSON in the admin UI on save, not at profile-save time","Programmatic setting updates must use JSON.stringify","Remember empty/whitespace-only is treated as no custom fields, not an error"],"tags":["settings","custom-fields","json","configuration","user-profile"],"backgroundTag":"invalid-json-config","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","contentChangedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}