Fetching select-type rollup property values from Notion API

I’m stuck trying to get a rollup property value that’s a ‘select’ type in Notion. Here’s what I’ve tried:

const rollupFieldName = 'myRollup';
const propDetails = await notionClient.pages.properties.retrieve({
  page_id: currentPage.id,
  property_id: currentPage.properties[rollupFieldName].id,
});

The currentPage.properties has this rollup info:

myRollup: {
  id: 'Ab%3Xy',
  type: 'rollup',
  rollup: { type: 'array', array: [], function: 'show_original' }
},

But I keep getting this error:

{"object":"error","status":400,"code":"validation_error","message":"Invalid relations found in rollup."}

Any ideas on how to grab that select value? I’m pretty confused about what I’m doing wrong here. Thanks for any help!

I’ve dealt with this exact problem in one of my projects. The key is understanding that rollup properties, especially select types, can’t be accessed directly through the page properties endpoint.

Instead, you need to query the database that contains the source data for the rollup. Here’s what worked for me:

  1. First, identify the source database ID for your rollup.
  2. Use the databases.query endpoint to fetch the relevant rows.
  3. Apply a filter to get only the row(s) related to your current page.
  4. Extract the rollup value from the query results.

Here’s a code snippet that should work:

const sourceDbId = 'your_source_database_id';
const response = await notionClient.databases.query({
  database_id: sourceDbId,
  filter: {
    property: 'Related Page',
    relation: {
      contains: currentPage.id
    }
  }
});

const rollupValue = response.results[0].properties[rollupFieldName].rollup.array[0].select.name;

This approach bypasses the validation error you’re encountering. Just make sure to replace ‘your_source_database_id’ and ‘Related Page’ with your actual database ID and the name of the property that links to your current page.

i had a similar issue. try using the /databases endpoint instead of /pages. something like:

const db = await notion.databases.query({
  database_id: your_db_id,
  filter: { property: 'Page', equals: currentPage.id }
});

then access the rollup from db.results[0].properties. hope this helps!

I’ve encountered this issue before. The problem lies in how Notion handles rollup properties. For select-type rollups, you need to query the database that contains the rolled-up data, not the page itself.

Try this approach:

  1. Identify the source database for your rollup.
  2. Query that database, filtering for the current page.
  3. Extract the rollup value from the query results.

Here’s a rough code snippet:

const sourceDbId = 'your_source_db_id';
const response = await notionClient.databases.query({
  database_id: sourceDbId,
  filter: {
    property: 'Linked Property Name',
    relation: {
      contains: currentPage.id
    }
  }
});

const rollupValue = response.results[0].properties[rollupFieldName].rollup.array[0].select.name;

This method should retrieve the select value you’re after. Let me know if you need further clarification.