Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Updating a field value of another document when clicking over a boolean type in Sanity.io.

12 replies
Last updated: Nov 2, 2021
Hello Everyone! I have a question. How can i update a field value of another document when clic over a boolean type? I have the following schema:
export default {
    name:"myschema",
    type:"document",
    title:"My Schema",
    description:"MySchema",
    fields:[
        {
            name:"active",
            title:"Active",
            type:"boolean"
        },
        {
            name:"name",
            title:"Event Name",
            type:"string"
        }
    ] 
}
The idea is that only one document can have the active field as true without the editor having to manually unchecked the previous selected event.
Nov 2, 2021, 7:44 PM
Hi User. This approach might get more complicated as you consider other parts of the picture, such as changes made outside of the studio (e.g., using the mutations API) that will not adhere to validation or document actions in the studio. If I might make a suggestion, I wonder if you might consider a different way of setting the active document, which is to have a singleton (e.g., a settings page) with a reference to a document. That document is the one considered “active” for this purpose, and when someone wants to change it they do so on that settings page (without worrying about conflicts, unsetting first, etc.). This would also permit changes to be made via the CLI or API.
If you want to stick with your current boolean approach, one method might be using validation (check if any other documents have the boolean set to true, and prevent publication if so). Another might be
document actions , where you could unset the field in the other document when you publish this one.
Nov 2, 2021, 7:56 PM
Hi
user A
! Thanks for your response! This is my first time hearing about the singleton approach. Where can i find more information about it?
Nov 2, 2021, 7:58 PM
This is a good guide. Please let me know if you face any struggles and we’ll get your through them.
Nov 2, 2021, 8:12 PM
This is a good guide. Please let me know if you face any struggles and we’ll get your through them.
Nov 2, 2021, 8:12 PM
Great! Thank you!
Nov 2, 2021, 8:13 PM
This is a good guide. Please let me know if you face any struggles and we’ll get your through them.
Nov 2, 2021, 8:12 PM
I watched the video and it could work but i have question: If i create a 'Setting Page', does that mean that i will have to query the setting document instead of the event document?
Nov 2, 2021, 8:38 PM
Yes, you’d need to query it in some form. If your current query were:

*[_type == 'myschema' && active == true]
Your new one might be something like:


*[_type == 'myschema' && *[_type == 'settings' && active._ref == ^._id]]
Nov 2, 2021, 8:43 PM
Yes, you’d need to query it in some form. If your current query were:

*[_type == 'myschema' && active == true]
Your new one might be something like:


*[_type == 'myschema' && *[_type == 'settings' && active._ref == ^._id]]
Nov 2, 2021, 8:43 PM
That inner query would only return true in one instance—where the
_ref
property on the settings document’s
active
field is equivalent to the
_id
of the document being “considered” (as far as looping over documents goes).
Nov 2, 2021, 8:44 PM
Got it. I wasn't aware of the 'setting page' approach. That changes a lot of my schemas designs(luck me we are not in production with enough time to make those quick changes). THANK YOU User!!
Nov 2, 2021, 8:50 PM
You’re welcome! As I said, it’s not the only way to go, but I can anticipate problems with the boolean approach that are solved this way (and hopefully some time soon a release will make things even easier 😉).
Nov 2, 2021, 8:52 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?