Setting Default Values for New Records
Overview
This guide demonstrates patterns for automatically populating default values when users create new records in Maximo Mobile applications. These techniques ensure data consistency, reduce user input errors, and enforce business rules at the point of record creation.
Use Cases:
- Standardize field values across record creation based on organizational policies
- Pre-populate records with values inherited from parent records
- Reduce data entry time and errors by providing sensible defaults for required fields
- Enforce business rules and data validation at creation time
Prerequisites:
- Basic understanding of Maximo Mobile datasources and schemas
- Familiarity with page controllers and lifecycle methods
- Access to the MAF Configuration application
- Knowledge of object structure attributes for your target entity
Implementation
Step 1: Define configuration constants
Create constants to store default values rather than hardcoding them throughout your code. This centralized approach improves maintainability and makes configuration changes easier.
JavaScript Implementation:
/*** Default values for record creation* Centralize configuration for easy maintenance and updates*/const DEFAULT_FIELD_ONE = 'VALUE_A'; // Description of what this representsconst DEFAULT_FIELD_TWO = 1; // Description of what this represents
Key Takeaways:
- Centralization: Keep all default values in one location for easy updates
- Documentation: Add comments explaining the meaning of coded values
- Naming: Use descriptive constant names that clearly indicate their purpose
- Flexibility: Consider making these configurable through app settings for different environments
Expected Result: Constants are defined and can be referenced throughout the controller for consistent default value application.
Step 2: Choose the most appropriate alternative from easiest to hardest
Choose the most appropriate alternative based on your specific requirements and constraints.
Approach 1: Configure datasource schema with default values
Set default values directly in the datasource schema for new record creation. This declarative approach applies automatically when new records are added to the datasource.
XML Configuration:
<!-- Datasource schema with default values --><maximo-datasource id="dsCreateRecord" object-structure="MXAPI[OBJECTNAME]"><schema><!-- Field with default value --><attribute name="fieldOne" id="attr_001" default-value="VALUE_A"/><!-- Another field with default value --><attribute name="fieldTwo" id="attr_002" default-value="1"/>
Key Takeaways:
- Declarative Approach: Schema defaults are the simplest method for static values
- Automatic Application: Defaults apply to all new record creation scenarios using this datasource
- Early Initialization: Schema defaults are applied before any programmatic logic runs
- Type Awareness: Default values should match the attribute’s data type
- Scope: This method works universally across the application for the datasource
Expected Result: When a new record is created through this datasource, the specified fields are automatically initialized with the configured default values.
Approach 2: Set defaults programmatically using onAddNewRecord
For scenarios requiring conditional logic or dynamic defaults, implement the onAddNewRecord event handler in your datasource controller.
JavaScript Implementation:
/*** Add new record handler* Sets default values dynamically when creating new records* @param {Object} params - Event parameters* @param {Datasource} params.datasource - The datasource instance* @param {Object} params.item - The new record item*/onAddNewRecord({datasource, item}) {const TAG = 'SetDefaultValues';
Key Takeaways:
- Dynamic Defaults: Use this approach when defaults need to be calculated or depend on application state
- Schema Properties: Modify
schema.properties[].defaultValueto ensure values are treated as schema defaults - Event Timing: This method runs before the record is displayed to the user
- Datasource Identification: Check datasource name to apply defaults only where intended
- Avoid Direct Assignment: Don’t set values directly on the
itemobject to prevent conflicts with schema validation - Conditional Logic: Implement business rules to determine appropriate defaults based on context
Expected Result: Default values are applied dynamically each time a new record is added to the datasource, allowing for conditional logic based on application state or user context.
Validation & Testing
How to verify the configuration works:
Test new record creation:
- Navigate to the record list page
- Click the “Create New Record” button
- Expected Result: Configured fields show default values automatically
Verify defaults can be overridden:
- Create a new record with defaults applied
- Change a defaulted field to a different value
- Save the record
- Expected Result: The record saves with the user-selected value, not the default
Check console logs:
- Enable debug logging in the application
- Expected Result: Console shows log messages confirming default values were applied
Common Issues:
Issue: Schema default values are ignored
- Cause: The datasource ID in XML doesn’t match the datasource being used for creation
- Solution: Confirm the datasource
idattribute matches the datasource referenced in your page. Use browser developer tools to inspect the datasource name.
Issue: Programmatic defaults conflict with schema defaults
- Cause: Both schema
default-valueandonAddNewRecordare setting the same field - Solution: Choose one approach per field. Use schema defaults for static values and
onAddNewRecordfor dynamic values.
- Cause: Both schema
Issue: Defaults not persisting when saving
- Cause: Validation rules or server-side logic may be overriding the values
- Solution: Check Maximo object structure validation rules and ensure the default values are valid for the target object.
Best Practices
✅ Do:
- Use constants for default values to centralize configuration and improve maintainability
- Add descriptive comments explaining the meaning of coded values and business rules
- Implement logging to track when defaults are applied for troubleshooting
- Use schema
default-valuefor simple static defaults that don’t change - Use
onAddNewRecordfor complex or conditional default logic - Validate defaults comply with Maximo business rules and validation requirements
- Document your approach so other developers understand the default value strategy
❌ Don’t:
- Hardcode values directly in multiple places throughout the code
- Set defaults on
itemdirectly inonAddNewRecord(use schema properties instead) - Assume defaults always apply without testing edge cases and error scenarios
- Override user-entered values after they’ve been modified by the user
- Skip null checks when accessing page parameters or nested objects
- Mix approaches (schema defaults and programmatic defaults) for the same field without clear reasoning
- Forget error handling when calculating dynamic defaults
Performance Considerations:
- Schema defaults have minimal performance impact as they’re applied during record initialization
- Avoid complex calculations in
onAddNewRecordas it runs for every new record - Consider caching computed default values if they’re expensive to calculate
- Be mindful of datasource size when applying defaults to multiple fields
Security Considerations:
- Ensure default values comply with user permissions and security groups
- Don’t set defaults that bypass required validation or business rules
- Validate that users have permission to create records with the specified values
- Consider whether defaults should vary based on user role or security profile
- Audit default value changes to track configuration modifications
Pattern Comparison
When to Use Each Approach
Schema default-value Attribute:
- ✅ Static values that never change
- ✅ Simple configuration without conditional logic
- ✅ Values that apply universally across all creation scenarios
- ✅ When you want declarative, self-documenting configuration
- ❌ Dynamic values that depend on application state
- ❌ Conditional defaults based on user role or context
onAddNewRecord Datasource Event Handler:
- ✅ Dynamic defaults calculated at runtime
- ✅ Conditional logic based on application state
- ✅ Defaults that depend on user role, permissions, or preferences
- ✅ Complex business rules for default value determination
- ✅ When you need to access other datasources or app state
- ❌ Simple static values (use schema defaults instead)
Related Resources
Prerequisites:
- Understanding Datasources - Learn about datasource configuration and schemas
- Page Controllers and Lifecycle Methods - Understand controller methods and event handlers
Related Practices:
- Making Fields Read-Only - Control field editability based on conditions
- Marking Required Fields - Ensure critical fields are populated
- Client-Side Datasource Filtering - Filter datasource records based on criteria
Next Steps:
- Additional Logic for Records - Implement complex business rules
- Validation and Error Handling - Add validation logic for user input
External Documentation:
- IBM Maximo Mobile Framework Documentation - Official MAF documentation
- Maximo Application Framework - Core concepts and configuration
Additional Notes
Version Compatibility:
- Schema
default-valueattribute is supported in all MAF versions - The
onAddNewRecordevent handler is available in MAF 8.2+
Mobile Considerations:
- Default values are applied consistently across mobile and web interfaces
- Offline mode: Defaults are applied locally and synced when the connection is restored
- Consider network latency when defaults depend on server-side data
- Test default value behavior in both online and offline scenarios
Advanced Tips:
- Environment-specific defaults: Use app configuration settings instead of hardcoded constants for values that vary by environment
- User-specific defaults: Check
app.client.userInfoto apply defaults based on user role or preferences - Parent record data: Use the
itemparameter inonAddNewRecordto access parent record data for intelligent defaults - Reusable utilities: Create utility functions for applying defaults across multiple datasources
- Async defaults: If defaults require async operations (API calls), handle promises appropriately in
onAddNewRecord
Troubleshooting:
- Enable debug logging: Set
app.log.level = 'debug'in your controller to see detailed logs - Inspect datasource state: Use browser developer tools to examine datasource state and schema properties
- Network monitoring: Check the network tab to verify record creation requests include default values
- Server logs: Review Maximo application logs for server-side validation errors that might reject defaults
- Breakpoints: Use debugger breakpoints in
onAddNewRecordto step through default value logic
Example: Work Order Scenario
Here’s how these patterns apply to a specific work order use case:
// Constants for work order defaultsconst DEFAULT_WORKTYPE = 'CM'; // Corrective Maintenanceconst DEFAULT_WORKPRIORITY = 1; // High priority// Schema configuration<attribute name="wopriority" id="ye34g" default-value="1"/><attribute name="worktype" id="y6nm_" default-value="CM"/>// Dynamic defaults in onAddNewRecord
This example demonstrates both patterns working together to ensure consistent default values across different work order creation scenarios.