AI Agent in Notion - getting dynamic body

Hello together, I’m building a Notion integration in n8n and trying to filter tasks based on dynamic inputs. I have a “Search inside Task DB” HTTP node that should filter tasks by customer_id, project_id, or both – depending on the input.

Current Workflow:

In my setup, these values are dynamically retrieved from prior nodes (Get Customer Page ID and Get Project Page ID). However, I cannot guarantee that both values are always present. Sometimes only the customer or only the project is provided.

If both values are present, the filter works correctly and returns the expected results.

But if one value (e.g., project_id) is missing or empty, the filter still includes it – and the request fails with a 400 error. I want to dynamically omit missing filters and let the query run with only the available ones.

What is the error message (if any)?

HTTP 400 – Bad request: please check your parameters

I’m using a Function node to construct the dynamic body like this (but the dynamic body doesn’t work yet):

const andConditions = ;

if ($json.customer_id) {
andConditions.push({
property: “Verknüpfung zu Kunden”,
relation: {
contains: $json.customer_id
}
});
}

if ($json.project_id) {
andConditions.push({
property: “Verknüpfung zu Projekt”,
relation: {
contains: $json.project_id
}
});
}

return [{
filter: {
and: andConditions
}
}];


The only body that is working for me right now is this (with placeholders):

{
“filter”: {
“and”: [
{
“property”: “Verknüpfung zu Kunden”,
“relation”: {
“contains”: “{customer_id}”
}
},
{
“property”: “Verknüpfung zu Projekt”,
“relation”: {
“contains”: “{project_id}”
}
}
]
}
}

The http request for search inside the task database:

But here I only can use it when both values are provided.
If both IDs are present, the response is correct and filtered.

If either customer_id or project_id is missing (empty string or undefined), the request fails with HTTP 400 because I used placeholders.

What I want
I want this filter logic to be robust and flexible:

If both IDs are present → filter by both

If only one is present → filter by that one

If neither is present → return all open tasks

Can you please tell me what I should change to get dynamic bodies?
Thank you very much!

1 Like

See if this works for you

1 Like

Hello. Thank you very much for your detailed workflow! I got your idea.

However, I tried to re-create this but I am getting errors.

My workflow:

It is working that it retrieves the right customer and project id (I guess :D). At the right you see that it searches for project “Ads” and gets the right data.

But still somehow it is not getting the tasks right.
I think it is because of the prompting?

Here is my system prompt for my ai agent:

Find Tasks in Notion Project

You are an AI agent that helps users retrieve tasks from a Notion database. Your job is to extract all necessary information from user’s message and use if for retrieving corresponding tasks.

Tools available:

  • Get Customer: use this tool to retrieve an id of a given Customer
  • Get Project: use this tool to retrieve an id of a given Project
  • Get Tasks: use this to retrieve Tasks with a given status using JSON filter

Step-by-step Workflow

First Step:

  • Extract Customer Name from User Message
    • Use natural language understanding to extract the intended customer name from the user query.
    • If Customer name is provided, use Get Customer tool to retrieve the id of a given customer (sometimes it only contains half of the customer name and not the exact name → search for suitable pages with the provided customer name).
    • If Customer name is not provided, continue with the next step. (DON’T USE THE TOOL THEN).
    • It is very important to take the overall page id and not the page id from property relation fields! The best way to do is to check for the page url and then use the number behind the name: Notion → ID: 23a079c42c578085a662d52b31c8377c (use the ID without hyphens)

Second Step:

  • Extract Project Name from User Message
    • Use natural language understanding to extract the intended project name from the user query.
    • If Project name is provided, use Get Project tool to retrieve the id of a given project (sometimes it only contains half of the project name and not the exact name → search for suitable pages with the provided project name).
    • If Project name is not provided, continue with the next step. (DON’T USE THE TOOL THEN).
      • It is very important to take the overall page id and not the page id from property relation fields! The best way to do is to check for the page url and then use the number behind the name: Notion → ID: 23a079c42c57805e8e2aff52d7ebaf3e (use the ID without hyphens)

After using the Get Customer and Get Project tool, pass the result’s page ID into the variable customer_id and project_id, so it can be reused in the Get Tasks tool.

Step 3: Build JSON filter for Get Tasks

Only include filter blocks for values that are found. Do not include empty or undefined properties.

Example output:
{
“filter”: {
“and”: [
{
“property”: “Project”,
“relation”: { “contains”: “abc123” }
},
{
“property”: “Customer”,
“relation”: { “contains”: “xyz456” }
}
]
}
}

For example

  • If customer_id is found, include this block:

{
“property”: “Customer”,
“relation”: {
“contains”: “<customer_id>”
}
}

If project_id is found, include this block:
{
“property”: “Project”,
“relation”: {
“contains”: “<project_id>”
}
}

Fourth Step:

  • Use Get Tasks tool to find the Tasks using compiled JSON as filter
  • Sort and Present found tasks to the User
    • Use markdown format for providing links for each listed task.

Final Output Format

If one of the values is not found, exclude it from the final output.

Generate the filter as a real JSON object, not as a string.

Correct:

{
“and”: [
{
“property”: “Project”,
“relation”: {
“contains”: “23b079…”
}
}
]
}

Incorrect (do not do this):

“{"and": [{"property": …”

Additional Guidelines

  • If asking the user for clarification, be concise and focused.
  • Never assume or guess the project name, customer name or task status, if unsure — always ask for confirmation.



What do you think what is wrong at this case?

Here are more details of my settings:

I think I got it.

can you please check this prompt if you can find any errors or issues that can result into errors? appreciate it!

And one more thing:
How can I add the requirement to check if a task is done or not?
I have a checkbox and I would like to filter for it too. What do I need to adjust on the code to also filter for done tasks if I ask in the initial message for it?

See on the left (that is my database property):


My Prompt:

Find Tasks in Notion Project:
You are an AI agent that helps users retrieve tasks from a Notion database. Your job is to extract all necessary information from user’s message and use it for retrieving corresponding tasks.

Tools available:
Get Customer: use this tool to retrieve an id of a given Customer
Get Project: use this tool to retrieve an id of a given Project
Get Tasks: use this tool to retrieve tasks using JSON filter

Step-by-step Workflow:

  1. Extract Customer Name from User Message
  • Use natural language understanding to extract the intended customer name from the user query.
  • If Customer name is provided, use Get Customer tool to retrieve the id of a given customer (for getting the ID you have to use the page URL to identify the correct ID:
    For example: Notion → the correct id is the last part of it: 23a079c42c578085a662d52b31c8377c - use this ID for next steps).
  • If Customer name is not provided, continue with the next step (don’t even use Get Customer Tool and straight away get to the next step).
  1. Extract Project Name from User Message
  • Use natural language understanding to extract the intended project name from the user query.
  • If Project name is provided, use Get Project tool to retrieve the id of a given project (for getting the ID you have to use the page URL to identify the correct ID:
    For example: Notion → the correct id is the last part of it: 23a079c42c57805e8e2aff52d7ebaf3e - use this ID for next steps).
  • If Project name is not provided, continue with the next step.
  1. Get Tasks from Tool
  • Use Get Tasks tool to find the Tasks using JSON below as the filter (make sure to include Customer property if Customer id was retrieved from Get Customer tool, make sure to include Project property if Project id was retrieved from Get Project tool).

{
“and”: [
// use this condition if the customer id is determined, otherwise exclude this property
{
“property”: “Verknüpfung zu Kunden”,
“relation”: {
“contains”: “”
}
},
// use this condition if the project id is determined, otherwise exclude this property
{
“property”: “Verknüpfung zu Projekt”,
“relation”: {
“contains”: “”
}
}
]
}

Use Get Tasks tool for retrieving all tasks matching the filter criteria

  1. Extract information required by User from the list of Tasks:
  • When extracting information, use {{ $now }} as reference to the date and time right now.
    Find relevant tasks based on Customer and/or Project if specified.
  • If no specific criteria were provided, return all available tasks.
  • Find relevant task information from each retrieved task
    Provide information requested by the user or further filter the list of tasks.
    Sort found tasks by Task ID.
  • Respond to the user with the list in a clear, human-readable format, including task name, customer name, project name, and any other relevant task details
    Use markdown format for providing links for each listed task.

Additional Guidelines:

  • If asking the user for clarification, be concise and focused.
  • Never assume or guess the project name, customer name, or task details, if unsure — always ask for confirmation.

Essentially, you would need to add and populate an additional property checker in the filter:

// include this if ...
{"property": "Done?", "checkbox": {"equals": true}}

and in the prompt you instruct the agent to add this is requested by the user.

If these answers were helpful, kindly mark one as solution. Thank you.

Cheers.

1 Like

Thank you so much! You are a big help!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.