From 1055764fff209fc95fa87917d92810b92d274d79 Mon Sep 17 00:00:00 2001 From: Etienne <45695613+etiennejouan@users.noreply.github.com> Date: Tue, 30 Jun 2026 10:01:24 +0200 Subject: [PATCH] fix: reconcile metadata store after object creation so activity targets on new custom objects link correctly (#22331) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Creating a Task or Note on a record of a newly-created custom object failed to link them, and the console showed `Missing field 'targetTest…' while writing result` for `TaskTarget`/`NoteTarget`. After creating a custom object, the front-end metadata store was left with an inconsistent morph relation group on the default-relation objects (`taskTarget`, `noteTarget`, `attachment`, `timelineActivity`). This reconciles the store from the server after creation so the morph fields are rebuilt correctly. ## Context / root cause The DB and server are correct: the new object adds a single member (e.g. `targetTest`) to the existing `target` morph group (shared `morphId`), and the server's `objects` query collapses + renames the group to one `target` field with a full `morphRelations` array. On the client, though, the metadata store is updated incrementally after creation: - The bulk `objects` query stores morph fields already collapsed (`target` + `morphRelations`). - The new reciprocal morph member arrives via SSE/mutation as a raw, un-collapsed field row (`targetTest`, without `morphRelations`), which `objectMetadataItemsWithFieldsSelector` simply joins in. This leaves two morph fields on `taskTarget`/`noteTarget` (`target` with stale members + an un-normalized `targetTest`). `mapFieldMetadataToGraphQLQuery` then fans `targetTest` out into non-existent fields (`targetTestCompany`, `targetTestPerson`, …), which the server omits, breaking the optimistic cache write (`writeFragment`) and leaving the activity target unlinked in the UI. This is a regression from the metadata-store incremental-sync refactor (the create path stopped reconciling reciprocal morph fields on existing objects). ## Fix In `useCreateOneObjectMetadataItem`, after the incremental store updates, call `invalidateMetadataStore()` so the objects/field metadata is refetched from the server and the morph groups are rebuilt in their correct collapsed form. This mirrors the existing pattern in `useDeleteOneObjectMetadataItem`. ## Test plan - [ ] Create a new custom object. - [ ] Open a record of that object and create a Task and a Note from it. - [ ] Verify no `Missing field 'target…'` error in the console and the task/note is linked (visible in the record's Tasks/Notes and on the activity target). - [ ] Confirm existing standard objects (Company/Person/Opportunity) still link tasks/notes correctly. - [ ] Confirm object creation still updates the left nav / views as before. Review in cubic --- .../object-metadata/hooks/useCreateOneObjectMetadataItem.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/twenty-front/src/modules/object-metadata/hooks/useCreateOneObjectMetadataItem.ts b/packages/twenty-front/src/modules/object-metadata/hooks/useCreateOneObjectMetadataItem.ts index ace4b5995f..27d605371f 100644 --- a/packages/twenty-front/src/modules/object-metadata/hooks/useCreateOneObjectMetadataItem.ts +++ b/packages/twenty-front/src/modules/object-metadata/hooks/useCreateOneObjectMetadataItem.ts @@ -7,6 +7,7 @@ import { FindManyViewsDocument, } from '~/generated-metadata/graphql'; +import { useInvalidateMetadataStore } from '@/metadata-store/hooks/useInvalidateMetadataStore'; import { useMetadataErrorHandler } from '@/metadata-error-handler/hooks/useMetadataErrorHandler'; import { useUpdateMetadataStoreDraft } from '@/metadata-store/hooks/useUpdateMetadataStoreDraft'; import { type FlatFieldMetadataItem } from '@/metadata-store/types/FlatFieldMetadataItem'; @@ -30,6 +31,7 @@ export const useCreateOneObjectMetadataItem = () => { const { enqueueErrorSnackBar } = useSnackBar(); const { addToDraft, replaceDraft, applyChanges } = useUpdateMetadataStoreDraft(); + const { invalidateMetadataStore } = useInvalidateMetadataStore(); const { loadCurrentUser } = useLoadCurrentUser(); const createOneObjectMetadataItem = async ( @@ -123,6 +125,8 @@ export const useCreateOneObjectMetadataItem = () => { applyChanges(); await loadCurrentUser(); + + invalidateMetadataStore(); } return {