Skip to content

Parent App Integration

ProcessFactorial Forms can be embedded in a parent application — typically a React single-page app. This article documents the contracts the parent app must fulfil for the form to work correctly.

1. Provide an authentication token (required)

For authenticated forms, the parent app must expose window.pfGetJWTToken as a global function. The Form Engine calls this whenever it needs a JWT token to authorise API requests.

Required for authenticated forms

If window.pfGetJWTToken is not defined, the form throws an error and cannot render.

Contract

/**
 * @returns {Promise<string> | string} A JWT token string
 */
window.pfGetJWTToken = async () => {
    // Return the current user's JWT token from your auth system
    const token = await yourAuthProvider.getToken();
    return token;
};

Example with MSAL (React)

import { useMsal } from '@azure/msal-react';
import { useEffect } from 'react';

function App() {
    const { instance, accounts } = useMsal();

    useEffect(() => {
        window.pfGetJWTToken = async () => {
            const response = await instance.acquireTokenSilent({
                scopes: ['api://your-app-registration/access_as_user'],
                account: accounts[0]
            });
            return response.accessToken;
        };
    }, [instance, accounts]);

    // ... render the DEM snippet
}

2. Receive host events from the form (optional)

The Form Engine sends postMessage events to the parent window when the user navigates, changes fields, or triggers custom button actions.

For the full message schema and a copy-paste listener example, see Parent Event Notifications.


3. Use the Forms Engine API (optional)

window.pfFormsEngine.v1 is available for reading and writing field values, injecting custom HTML, controlling element visibility, and triggering form events programmatically.

In addition to the value/HTML/visibility helpers, the API can execute logical form actions — default next/previous navigation, default save/submit/cancel, navigation to a specific page, or running an entire event pipeline — via executeAction(action, options). Arbitrary event definitions (e.g. a Navigation event targeting a page) can be executed directly with executeEvent(eventDefinition, options).

For the complete API reference and examples, see Injecting Custom HTML.


4. Refresh selected fields before Continue Existing (optional)

If an embedded form has resume field updates enabled, it asks the parent app for replacement values when a user selects Continue Existing from search results. The parent app may respond with values for the fields selected in the form's Search settings. The form uses those values before continuing the draft.

The listener is optional. When the parent app does not respond, the draft continues with its saved values after a short wait. Configure this feature only for values your app can safely use in that case.

For an iframe embed, register a message listener before showing the form. This example assumes formFrame is the iframe, formOrigin is its exact origin, and the two field IDs are the fields selected in the form settings:

window.addEventListener('message', async (event) => {
    if (event.origin !== formOrigin || event.source !== formFrame.contentWindow) return;
    const request = event.data;
    if (request?.type !== 'processfactorial:resume-data-transform-request' ||
        request.version !== 1 || !Array.isArray(request.allowedFieldIds)) return;

    // Acknowledge immediately, before any asynchronous token refresh.
    formFrame.contentWindow.postMessage({
        type: 'processfactorial:resume-data-transform-accepted',
        version: 1,
        requestId: request.requestId,
        recordId: request.recordId,
        formToken: request.formToken,
    }, formOrigin);

    const candidateValues = {
        'your-token-field-id': await getCurrentToken(),
        'your-device-field-id': getCurrentDeviceType(),
    };
    const overrides = Object.fromEntries(
        Object.entries(candidateValues).filter(([fieldId]) =>
            request.allowedFieldIds.includes(fieldId))
    );

    formFrame.contentWindow.postMessage({
        type: 'processfactorial:resume-data-transform-response',
        version: 1,
        requestId: request.requestId,
        recordId: request.recordId,
        formToken: request.formToken,
        overrides,
    }, formOrigin);
});

Send the acknowledgement immediately, then return the new values promptly. The form allows more time for a parent app that acknowledges the request. If the response arrives after the form has continued, it is ignored. Only the fields selected in the form settings can be changed this way.


Troubleshooting

For detailed guidance on common integration issues, see the dedicated troubleshooting articles: