Skip to main contentMAF Configuration Practices

Add Additional Logic to Existing Methods

Overview

Sometimes you need to add extra logic to an existing controller method without completely replacing it. This approach allows you to extend the out-of-the-box functionality by subscribing to the same event and adding your custom processing alongside the original method.

Use Cases:

  • Adding logging to track method execution
  • Performing additional validation before/after an action
  • Triggering side effects when certain events occur
  • Extending functionality without modifying core controllers

Prerequisites:

Important Note:

⚠️ The execution order of methods with the same name cannot be guaranteed. All methods subscribed to an event are called, but the sequence is not enforced by the platform. If order matters, use Replace Existing Method instead.

Implementation

Step 1: Identify the method to extend

Find the event or method you want to add logic to. This can be done by:

  1. Inspecting the XML - Look for event handlers in your app.xml:
<!-- Example: Dialog with secondary action -->
<dialog id="saveDiscardWorkLog"
on-secondary-action="closeWorkLogSaveDiscard"
secondary-action-text="Discard">
<!-- Dialog content -->
</dialog>
  1. Checking the controller - Review the out-of-the-box controller to understand what the original method does

  2. Using browser DevTools - Set breakpoints to see when methods are called

Step 2: Create the method in AppCustomizations.js

Add a method with the exact same name and signature as the original method:

AppCustomizations.js:

/**
* Custom Application Logic
*/
const TAG = 'CustomWorkLog';
class AppCustomizations {
/**
* Initialize application
* @param {Application} app - The application instance

Step 3: Test the implementation

  1. Trigger the event - Perform the action that calls the method
  2. Check logs - Verify your custom logic executes
  3. Verify original behavior - Ensure the out-of-the-box functionality still works
  4. Test edge cases - Try different scenarios to ensure stability

Complete Examples

Example 1: Adding Validation to Save Action

const TAG = 'CustomValidation';
class AppCustomizations {
applicationInitialized(app) {
this.app = app;
}
/**
* Add custom validation when saving work order

Example 2: Tracking Page Navigation

const TAG = 'NavigationTracker';
class AppCustomizations {
applicationInitialized(app) {
this.app = app;
this.navigationHistory = [];
}
/**

Example 3: Adding Logging to Multiple Events

const TAG = 'EventLogger';
class AppCustomizations {
applicationInitialized(app) {
this.app = app;
}
/**
* Log when datasource loads data

Validation & Testing

How to verify:

  1. Enable debug logging:

    ?_graphite_logLevel=debug
  2. Trigger the event:

    • Perform the action that calls the method
    • Check browser console for your log messages
  3. Verify both methods run:

    • Confirm out-of-the-box behavior works
    • Confirm your custom logic executes
  4. Test error scenarios:

    • What happens if your code throws an error?
    • Does the original method still execute?

Common Issues:

  • Issue: Custom logic doesn’t execute

    • Cause: Method name or signature doesn’t match exactly
    • Solution: Verify method name spelling and parameters
  • Issue: Original functionality breaks

    • Cause: Error in custom code prevents execution
    • Solution: Add try-catch blocks and proper error handling
  • Issue: Execution order matters but isn’t guaranteed

Best Practices

✅ Do:

  • Add comprehensive logging to track execution
  • Use try-catch blocks to prevent breaking original functionality
  • Document why you’re adding the logic
  • Test thoroughly in all scenarios
  • Keep custom logic focused and minimal
  • Use descriptive method names in logs

❌ Don’t:

  • Rely on execution order (it’s not guaranteed)
  • Modify parameters that affect the original method
  • Add heavy processing that impacts performance
  • Forget error handling
  • Assume your method runs before or after the original

Performance Considerations:

  • Keep additional logic lightweight
  • Avoid synchronous operations that block
  • Use async/await for I/O operations
  • Consider impact on mobile devices

Error Handling:

closeWorkLogSaveDiscard() {
try {
// Your custom logic
this.app.log.d(TAG, 'Custom logic executing');
this.performCustomAction();
} catch (error) {
// Don't let errors break the original functionality
this.app.log.e(TAG, 'Error in custom logic', error);
}

Comparison: Additional Logic vs Replace Method

AspectAdditional LogicReplace Method
ComplexitySimpleMore complex
Execution OrderNot guaranteedFully controlled
Original MethodRuns unchangedYou control if/when it runs
Use CaseSide effects, loggingModify core behavior
RiskLowHigher
MaintenanceEasierRequires updates on upgrades

When to use Additional Logic:

  • Adding logging or analytics
  • Triggering side effects
  • Validation that doesn’t block
  • Tracking user actions

When to use Replace Method:

  • Need to control execution order
  • Must modify original behavior
  • Need to prevent original execution
  • Complex conditional logic

Prerequisites:

Related Practices:

Next Steps:

External Documentation:

Additional Notes

Debugging Tips:

  1. Use unique log tags:

    const TAG = 'CustomFeature_MethodName';
  2. Log entry and exit:

    methodName() {
    this.app.log.d(TAG, 'Method started');
    // Your logic
    this.app.log.d(TAG, 'Method completed');
    }
  3. Log important values:

    this.app.log.d(TAG, `Processing item: ${JSON.stringify(item)}`);

Event Naming Conventions:

Some events don’t have aliases and must be quoted:

// Events with dashes need quotes
'field-warning'(event) {
// Handle field warning
}
'before-invoke-action'(event) {
// Handle before action
}
Page last updated: 13 January 2026