Default From Query is a REDCap External Module that allows REDCap fields to be auto-populated with values derived from SQL queries against REDCap's own database. Queries are defined by a system administrator at the project level and referenced by name from a field's action tag.
- REDCap >= 14.6.4
- PHP >= 8.2.0
-
Obtain this module from the Consortium REDCap Repo from the control center.
-
Go to Control Center > Manage External Modules and enable Default From Query.
-
Enable the module for each project that needs it via the project's Manage External Modules page.
Enable the module for the project via the project's Manage External Modules page.
In the project's External Modules configuration for Default From Query, add one or more query entries. Each entry requires:
| Field | Who can set it | Description |
|---|---|---|
| Query Name | Any project administrator | A short identifier used to reference this query in action tags |
| SQL | REDCap Admin only (project designers can view it on the Queries page) | A SQL statement that returns a single scalar value |
| Additional Project ID 1–3 | Any project administrator | Optional projects the SQL can reference as [pid-1]…[pid-3] |
| Allow on survey pages | REDCap Admin only | Lets the query run on survey pages (see Surveys) |
A REDCap administrator sees every setting:
Other project users with design rights see only the query names and additional project IDs:
In the Online Designer, add the following action tag to any field you want auto-populated:
@DEFAULT-FROM-QUERY='query_name'
where query_name matches the Query Name configured in project settings.
When a user opens a data entry form (or, for approved queries, a survey) for a record that has no existing data on that form, the module executes the associated query and injects the result as the field's default value:
The module configuration dialog hides the SQL from anyone who isn't a REDCap administrator. Users with Project Design and Setup rights can still read every query on the Default From Query: Queries page, linked from the project's left-hand menu under External Modules. For each query, the page shows:
- its name and SQL
- its additional project IDs, with project titles
- its survey status: Not allowed, Approved or Pending administrator approval
- the fields that reference it
The page is read-only. Only REDCap administrators can change the SQL, because the module runs it with REDCap's own database account.
Queries may reference the following Smart Variables, which are substituted with the current context before execution:
| Placeholder | Substituted with |
|---|---|
[project-id] |
The current project ID |
[field-name] |
The name of the field being populated |
[record-name] |
The current record ID (Note: For not-yet-saved records, the record ID will be null) |
[record-dag-id] |
The data access group id of the record |
[event-id] |
The current event id |
[current-instance] |
The current instance number |
[data-table] |
The REDCap data table for the current project (e.g. redcap_data). Data tables for other projects may be referenced by using the syntax [data-table:pid] where pid is a literal project ID or pid-1, pid-2, or pid-3, referencing the 1st, 2nd, or 3rd additional project ID set in the module settings for the query |
[pid-1], [pid-2], [pid-3] |
The project ID configured in the corresponding additional project ID field |
Example:
SELECT value
FROM [data-table]
WHERE project_id = [project-id]
AND record = [record-name]
AND field_name = [field-name]
ORDER BY instance DESC
LIMIT 1- Do not enclose any of these substitutions with quotes or the query will fail and return no value.
SELECT value
FROM [data-table]
WHERE project_id = [project-id]
AND record = '[record-name]' -- this will fail!
AND field_name = '[field-name]' -- this will also fail!
ORDER BY instance DESC
LIMIT 1- Values for
recordandfield_namemust be wrapped in quotes if you are not using the substitution value for that column value.
SELECT value
FROM [data-table]
WHERE project_id = [project-id]
AND record = [record-name]
AND field_name = 'some_other_field' -- Quotes are required in this context
ORDER BY instance DESC
LIMIT 1- If your query needs to query multiple project ids, record ids or field names, you will need to manage those differences as you write the query. The substitution values will always be in the context of the project, field, and record of the form as it is opened on the data entry page. References to up to three other projects may be specified via settings, but any further project ids or other field names will need to be hard-coded in the query.
select coalesce(max(max_visitnum) + 1, 1) as next_visitnum
from (
(
select max(cast(value as SIGNED)) as max_visitnum
from [data-table]
where project_id = [project-id]
and field_name = [field-name]
and record = [record-name] -- when you need only the value provided by the substitution, you can use it.
)
union
(
select max(cast(value as SIGNED)) as max_visitnum
from [data-table:pid-1] -- the data tables do not match so use a placeholder (set in module settings; alternatively, hard-code as, e.g., [data-table:123])
where project_id = [pid-1] -- the project_ids do not match so use a placeholder (set in module settings; alternatively, hard-code the project id)
and field_name = 'some_other_field'
and record = [record-name] --- the record name matches the one in the current project
)
) as dummy;@DEFAULT-FROM-QUERY also works on survey pages, but only for queries a REDCap administrator has approved for surveys. This is opt-in because survey respondents are not logged in, and anything a query returns is shown to them.
-
Approving a query: a REDCap administrator ticks Allow on survey pages on the query and saves the module configuration. That save approves the query exactly as it stands: its name, SQL, and additional project IDs.
-
Edits by non-admins void the approval: project users without admin rights can still change a query's name and additional project IDs. If they change either on an approved query, the query stops running on surveys immediately. It keeps working on data entry forms. A warning naming the query appears on the project's External Modules page and in the Online Designer, and the change is recorded in the project's Logging. To restore survey use, a REDCap administrator reviews the query and saves the module configuration again.
-
New records: on a public survey for a new record,
[record-name]is the auto-numbered ID REDCap has provisionally assigned. That record isn't saved yet, so queries that look up the current record's own data return nothing. Queries that look across records, such as a project-wide maximum, work as expected. -
Like data entry forms, surveys don't overwrite values that are already saved.
Approvals are stored as a module system setting (survey-approvals-pid-<project id>), which only REDCap administrators can change.
This module supports draft preview mode, but data retrieval through SQL queries is of course limited to values that are actually stored in the data table.
A prebuilt test project, SQL queries, and a test procedure are available in Testing Default From Query
The smart variable placeholders used in SQL queries were renamed in v2.0.0. If you have existing queries using the old names, update them before upgrading:
| Old placeholder | New placeholder |
|---|---|
[project_id] |
[project-id] |
[record_id] |
[record-name] |
[field_name] |
[field-name] |
[pid1] |
[pid-1] |
[pid2] |
[pid-2] |
[pid3] |
[pid-3] |
[data-table:pid1] |
[data-table:pid-1] |
[data-table:pid2] |
[data-table:pid-2] |
[data-table:pid3] |
[data-table:pid-3] |
Queries using old placeholder names will silently produce no value after upgrading to v2.0.0.
This module is derived from Auto Populate Fields, a REDCap External Module developed at the University of Florida. We thank the contributors of that project for their foundational work.
Apache 2.0 — see LICENSE.





