# Context Helper Documentation

The `context` object provides a powerful interface for accessing data and performing actions within the Report Static Query Builder. It's available globally as `window.context` and contains several key components.

### Structure Overview

```js
{
  params: {},       // Query parameters
  table: {},        // Available tables with data
  flow: Function,  // Load or open a flow
  form: Function,  // Open a form
  query: {}         // Query builder object
}
```

### Components

**params**

The `params` object contains all query parameters available to the current report.

```js
// Example
context.params.userId = "12345";
context.params.startDate = "2023-01-01";
```

**table**

The `table` object contains all available tables, where each table is a list of objects.

```js
// Example
const firstUser = context.table.users[0];
const totalSales = context.table.sales.reduce((sum, sale) => sum + sale.amount, 0);
```

**flow()**

The `flow()` function allows you to load or open another flow from the current report.

**Parameters:**

```js
context.flow({
  flowId: "flow123",                // Required: ID of the flow to load
  executionType: "loadFlow",        // Required: "loadFlow" or "openFlow"
  reloadCurrentFlow: true,          // Optional: Whether to reload the current page after loading the flow
  reloadCurrentContent: true,       // Optional: Whether to reload the current content after loading the flow
  resetParams: false,               // Optional: Whether to reset params to default after loading the flow
  params: [                         // Optional: Parameters to pass to the flow
    {
      paramName: "userId",
      paramValue: "12345"
    },
    {
      paramName: "view",
      paramValue: "detailed"
    }
  ]
});
```

**executionType options:**

* `openFlow`: Opens the chosen flow in the dashboard
* `loadFlow`: Loads the chosen flow in the background

**form()**

The `form()` function allows you to open a form from the current report.

**Parameters:**

```js
context.form({
  formId: "form123",               // Required: ID of the form to open
  params: [                        // Optional: Parameters to pass to the form
    {
      paramName: "userId",
      paramValue: "12345"
    },
    {
      paramName: "mode",
      paramValue: "edit"
    }
  ]
});
```

**query**

The `query` property provides access to the query builder object. This powerful feature allows you to construct and execute queries programmatically.

> **Note:** For detailed documentation on the query builder functionality, please refer to the Query Builder documentation in the other tab.

### Practical Examples

**Navigating between flows based on a condition:**

```js
if (context.table.users.length > 0) {
  context.flow({
    flowId: "userDetailsFlow",
    executionType: "flow",
    params: [
      {
        paramName: "userId",
        paramValue: context.table.users[0].id
      }
    ]
  });
} else {
  context.flow({
    flowId: "noUsersFlow",
    executionType: "openFlow"
  });
}
```

**Opening a form with data from the current context:**

```js
// Open an edit form for the first product in the table
context.form({
  formId: "productEditForm",
  params: [
    {
      paramName: "productId",
      paramValue: context.table.products[0].id
    },
    {
      paramName: "category",
      paramValue: context.params.currentCategory
    }
  ]
});
```

**Loading a flow in the background and then reloading the current page:**

```js
// Update data in the background and then refresh the current view
context.flow({
  flowId: "dataUpdateFlow",
  executionType: "loadFlow",
  reloadCurrentFlow: true,
  reloadCurrentContent: true,
  resetParams: false
});
```

### Best Practices

1. **Parameter Management**: When passing parameters to flows or forms, only include the parameters that need to change from the current context.
2. **Error Handling**: Always ensure that the required IDs (flowId, formId) are valid before calling the respective functions.
3. **User Experience**: Consider using `reloadCurrent: true` with caution as it will refresh the user's current view.
4. **Performance**: When working with large tables, consider processing the data before navigating to another flow to minimize the data transfer.
