
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeI can see you're struggling with using a GROQ query to set initial values dynamically. Let me clarify what's happening and provide the correct solution.
The "Failed to resolve initial value" error occurs because field-level initialValue in your schema doesn't execute GROQ queries. When you write a GROQ query string in initialValue, it's just a string - nothing is actually running that query against your content.
Field-level initialValue can be:
But it doesn't have built-in access to query your Sanity content with GROQ.
To set initial values based on GROQ queries, you need to use Initial Value Templates configured through the templates configuration in your sanity.config.ts:
// sanity.config.ts
import {defineConfig} from 'sanity'
export default defineConfig({
// ... other config
templates: (prev) => {
return [
...prev,
{
id: 'serviceCodeList-with-vt',
title: 'Service Code List (VT)',
schemaType: 'serviceCodeList',
value: async (params, context) => {
// Use the context client to query your content
const client = context.getClient({apiVersion: '2024-11-01'})
// Query for the VT code list - get just the first result's ID
const result = await client.fetch(
`*[_type == 'codeList' && code match 'VT'][0]._id`
)
return {
codeList: {
_ref: result,
_type: 'reference'
}
}
}
}
]
}
})Key points about this approach:
templates configuration in your config filecontext.getClient() to get a Sanity client that can execute GROQ queries[0] to get the first result instead of an array_id and construct the reference object in the return valueThe strange blinking/bumping behavior you're seeing with the hardcoded _ref suggests a validation or rendering issue. This commonly happens when:
Type confusion - In your query result, you mentioned uncertainty about whether _type should be "serviceCodeList" or "codeList". In a reference object's structure, _type should always be "reference", never the document type name:
{
codeList: {
_ref: '56d99a7f-bd55-4cd7-9eeb-d5b65fe63d5e',
_type: 'reference' // Always "reference", not the target document type
}
}Schema mismatch - Verify your schema field is properly defined as a reference type to codeList documents
Validation loops - Studio might be repeatedly fetching the referenced document to validate it exists
The smooth behavior in tutorials comes from using Initial Value Templates properly - they resolve once during document creation, then the value is stored. No repeated fetching or validation loops.
No, this isn't about document vs array - initialValue works for both. The issue is specifically about query execution context. Field-level initialValue can't execute GROQ queries; you need the template approach with context.getClient().
If the VT code list reference rarely changes, your hardcoded approach is actually perfectly valid:
initialValue: {
codeList: {
_ref: '56d99a7f-bd55-4cd7-9eeb-d5b65fe63d5e',
_type: 'reference',
}
}This isn't a "query" but it's a legitimate pattern if the reference is essentially constant. The blinking might resolve once you ensure the _type is correctly set to "reference" and save/reopen the document.
If you need dynamic lookup based on the code field matching "VT", use the Initial Value Templates approach in the templates configuration as shown above. If the VT reference is stable and won't change, the hardcoded field-level initialValue is simpler and perfectly acceptable - just make sure _type: 'reference' is correct and the schema field definition matches.
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store