Change field value for all existing records with a specific field value

Goal:

We need to remove a field option from a single line string field Block. The problem is that existing records point to that value, and we’d like to switch it for another. We thought the following process would be enough:

  1. Modify block to add new value option β€œy”
  2. Get all existing records of that block type that point to value β€œx” and change it for new value β€œy”
  3. Modify block again to remove existing β€œx” value.

Step 1 was completed successfully but step 2, we are stuck on.

Reading this documentation: https://www.datocms.com/docs/content-management-api/resources/item/instances we thought it was enough to do as such:

const imgCardModel = "img_cards_slim";
 const brightGrey = 'brightgrey';

// Update all records that have color grey to have brightgrey

    const records = await client.items.all({
        filter: {
            type: imgCardModel,
            fields: {
                colors: {
                    eq: 'grey',
                }
            },
        },
    },{allPages: true});

    for(const record of records){
        await client.items.update(record.id, {
            colors: brightGrey,
        });
    }

But this gives a 404 error from the API, stating that it can’t find that record. I’m not quite sure what is wrong?

Thank you!

Hello @technology

The problem is: "img_cards_slim" is not a Model Record, it is a Block Record, and block records can’t be updated with the client.items.update method.

Here is the documentation on how to update blocks: Update a record - Record - Content Management API

1 Like