Prepopulating Widgets with Default Values
When a form loads, the SDK calls Form_onInit(bool isInitialLoad) with isInitialLoad: true. This is the place to fetch data from your database and assign values via record.SetField() — the SDK will render whatever you set before returning the response to the frontend.
For existing records use this to load and display the saved data. For new records use it to push sensible defaults so the user starts with pre-filled fields.
When to use this
Default values are useful when:
- A field should inherit its value from the parent context (e.g. a department ID when adding an employee from within a department form).
- A date field should default to today.
- A status field should default to
"Draft"or"New". - The current user's name or ID should be pre-stamped.
Setting values in Form_onInit
Form_onInit is called on every request, but isInitialLoad is true only during the initial form load — use it to guard DB fetches that should only run once:
public override async Task Form_onInit(bool isInitialLoad)
{
await base.Form_onInit(isInitialLoad);
if (!isInitialLoad) return;
if (!record.IsNewByRequest())
{
// Existing record — fetch from DB and populate fields
var employee = await _db.Employees.FindAsync(Guid.Parse(record.GetRecordGuid()!));
if (employee != null)
{
record.SetField("firstName", employee.FirstName);
record.SetField("lastName", employee.LastName);
record.SetField("department", employee.DepartmentId.ToString());
record.SetField("hireDate", employee.HireDate.ToUniversalTime().ToString("o"));
}
}
else
{
// New record — set sensible defaults
record.SetField("status", "1");
record.SetField("createdDate", DateTime.UtcNow.ToString("o"));
record.SetField("createdBy", Context.CurrentUser?.DisplayName ?? "");
record.SetField("currency", "USD");
}
}
Inheriting from parent context
When a form is opened with a project GUID in context, Context.PGuid contains that value. Use it to look up and pre-fill related data:
public override async Task Form_onInit(bool isInitialLoad)
{
await base.Form_onInit(isInitialLoad);
if (record.IsNewByRequest() && !string.IsNullOrEmpty(Context.PGuid))
{
var customer = await _db.Customers.FindAsync(Guid.Parse(Context.PGuid));
if (customer != null)
{
record.SetField("customerId", customer.Id.ToString());
record.SetField("customerName", customer.Name);
record.SetField("billingAddr", customer.BillingAddress);
}
}
}
Prefill from the frontend
You can also push prefill data from the frontend when programmatically opening a form via the OpenModal command:
// Backend: open a child form with prefill data
OpenModal(new OpenModalArgs
{
FormCode = "ORDERFORM",
Guid = "new",
PrefillData = new Dictionary<string, string>
{
{ "customerId", customerId },
{ "customerName", customerName },
{ "orderDate", DateTime.Today.ToString("yyyy-MM-dd") }
}
});
These values arrive in the child form's Form_onInit and are accessible via record.GetField().
Priority of values
When the same field has a value in multiple sources, the final value is determined by this order (last wins):
- Buildocs base definition default
- Values from the opening form's
prefillData - Values set by your
FormInithandler