Skip to content

ProjectPatient onboarding with save-for-later

  • Industry

    Pharma

  • Cloud

    OmniStudio

  • Role

    OmniStudio developer

  • Period

    Independent, 2023 – present

The short version

  1. InheritedNested OmniScripts, built by earlier developers
  2. RequiredSave for Later, much later in the project
  3. EngineeredA custom Save button and a DataRaptor
  4. FoundOn resume, the custom LWCs ignored the restored data
  5. FixedGetters and setters in 20+ components

The setup

I picked up a pharma patient-onboarding build in OmniStudio where the earlier developers had put OmniScripts inside OmniScripts. Standard OmniStudio does not support that structure for this kind of journey: Salesforce’s page on the built-in Save for Later lists “OmniScripts that contain other OmniScripts (directly or indirectly)” as an unsupported configuration. That fits what I saw on this project, where the built-in option was not going to work.

PatternThe inherited structureThe approach, not a client’s system
containsrendersParent OmniScriptthe onboarding journeyNested OmniScripta step that is itself an OmniScriptCustom LWCseach with its own internal state

The pattern in outline, not a specification of the client's system.

The new requirement

Much later in the project, the business required Save for Later: a patient stops partway through, leaves, and comes back to the same place. OmniStudio’s built-in feature stores the data JSON as a saved session, but with the nesting in place I needed a route of my own.

What I engineered

I built a custom Save button that calls a DataRaptor, which writes all of the journey’s data onto the main object. A DataRaptor Load maps JSON onto Salesforce records, and the OmniScript’s own data JSON is what you feed it, as Trailhead’s Extract and Load exercise shows.

PatternSave for later, customThe approach, not a client’s system
data JSONsavesCustom Save buttonan LWC inside the OmniScriptDataRaptormaps the data JSON to fieldsMain objectone record per patient journey

The shape of the save path. The sketch below goes through an Integration Procedure, which is one way to reach the DataRaptor.

Illustrative: rebuilt for this site, not client codesaveForLaterButton.js
import { LightningElement, api } from 'lwc';
import { OmniscriptBaseMixin } from 'omnistudio/omniscriptBaseMixin';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

/** Namespace prefix with a trailing dot, if the org's OmniStudio runtime needs one. */
const NS = '';
/** Integration Procedure (Type_SubType) whose DataRaptor Post action writes the main object. */
const SAVE_PROCEDURE = 'Onboarding_SaveDraft';

export default class SaveForLaterButton extends OmniscriptBaseMixin(LightningElement) {
  /** Set in the designer's custom LWC properties so the draft knows where to resume. */
  @api stepName;

  saving = false;

  async handleSave() {
      if (this.saving) return;
      this.saving = true;
      try {
          const response = await this.omniRemoteCall(
              {
                  sClassName: `${NS}IntegrationProcedureService`,
                  sMethodName: SAVE_PROCEDURE,
                  input: JSON.stringify({ currentStep: this.stepName, data: this.omniJsonData }),
                  options: JSON.stringify({})
              },
              true // show the OmniScript spinner
          );
          if (response?.error) {
              throw new Error('The save request returned an error.');
          }
          this.toast('Progress saved', 'You can come back and continue later.', 'success');
      } catch (e) {
          this.toast('Could not save', e.message, 'error');
      } finally {
          this.saving = false;
      }
  }

  toast(title, message, variant) {
      this.dispatchEvent(new ShowToastEvent({ title, message, variant }));
  }
}

This is a sketch. The IP name, namespace and property names are placeholders, and calling an Integration Procedure from a component is thinly documented. The best public example I found is a community recipe whose README itself warns against leaning on the pattern, so test it against your org’s runtime before relying on it.

The real bug

Saving worked. Resuming did not, in the part that mattered. When a patient returned, the OmniScript’s own fields refilled, but the saved data did not flow back into the custom LWCs inside it. Their screens looked as if the patient had never been there, even though the data was in the OmniScript.

PatternResume, and where it brokeThe approach, not a client’s system
restoresnever arrivedPatient returnsopens the journey againDataRaptor Extractreads the main objectOmniScript data JSONfields refill hereCustom LWCsdid not pick up the data

In our org's setup, restored values reached the OmniScript's own elements but not the custom components.

Why: the components took their data once, when they were created, and kept a private copy. That is fine for a fresh run, where the data is already there at the start. A copy taken once never hears about data that is restored later. I could not find Salesforce documentation that describes this resume case, so treat the diagnosis as my own. The nearest thing I found is a community answer making a related point in the other direction: updating the data JSON from a component does not by itself refresh the OmniScript’s elements.

The demo is a simplified model of that behaviour. Save, leave and resume with Before my fix selected, then switch to After my fix and resume again.

Live demoSave for Later: before and after the getter/setter fix
Widget code
This demo needs JavaScript.
Data JSONdebug view
Patient_Onboarding__csaved record

A simplified model with a fictional patient, not the real components or OmniStudio internals. Everything runs in your browser.

The fix

I implemented getters and setters inside the LWCs, in at least 20 components. The rule is plain LWC, and the LWC developer guide puts it in one line: “To execute logic each time a public property is set, write a custom setter.” So the component no longer reads its data once. It exposes the value as an @api property, and every time the OmniScript hands it data, restored data included, the setter re-syncs the component’s state. Edits still flow the other way with omniApplyCallResp.

PatternResume with settersThe approach, not a client’s system
RestorePush into componentspushesre-syncsDataRaptor Extractreads the main objectOmniScript data JSONrestored@api setterruns on every pushComponent statematches the data again

The restore path is unchanged. The change is that each component now listens.

The component before the change, reading once:

Illustrative: rebuilt for this site, not client codemedicationList.js (before)
import { LightningElement, api } from 'lwc';
import { OmniscriptBaseMixin } from 'omnistudio/omniscriptBaseMixin';

export default class MedicationList extends OmniscriptBaseMixin(LightningElement) {
  @api stepName;

  items = [];

  connectedCallback() {
      // Runs once, when the component is inserted. Whatever the OmniScript holds at
      // this moment is copied. Data restored afterwards never reaches `items`.
      const saved = this.omniJsonData?.[this.stepName]?.Medications ?? [];
      this.items = saved.map((m) => ({ ...m }));
  }

  handleAdd(event) {
      const { name, dose } = event.detail;
      this.items = [...this.items, { name, dose }];
      this.omniApplyCallResp({ [this.stepName]: { Medications: this.items } });
  }
}

And after, with an @api getter and setter:

Illustrative: rebuilt for this site, not client codemedicationList.js (after)
import { LightningElement, api } from 'lwc';
import { OmniscriptBaseMixin } from 'omnistudio/omniscriptBaseMixin';

export default class MedicationList extends OmniscriptBaseMixin(LightningElement) {
  @api stepName;

  _items = [];

  /**
   * The OmniScript assigns this whenever it has data for the component, including data
   * restored on resume. The template iterates `medications`.
   */
  @api
  get medications() {
      return this._items;
  }
  set medications(value) {
      // Values passed down are read-only proxies, so keep a plain copy.
      this._items = Array.isArray(value) ? value.map((m) => ({ ...m })) : [];
  }

  handleAdd(event) {
      const { name, dose } = event.detail;
      this._items = [...this._items, { name, dose }];
      this.omniApplyCallResp({ [this.stepName]: { Medications: this._items } });
  }
}

Two details matter. @api goes on the getter or the setter, not both, and a setter needs its getter, as the docs state. And the setter only copies state. It does not call omniApplyCallResp, so a restore cannot echo back into the OmniScript and loop.

Results

  • 20+components fixed

A patient who leaves partway through can come back, and the journey reopens with their answers in both the OmniScript’s fields and the custom components. The lesson I took from it: a custom component inside an OmniScript should treat its input as something that can change at any moment, not as an initial value.

References

Official Salesforce documentation and Trailhead:

Community and third-party, not official:

Built with the Salesforce Lightning Design System. Not affiliated with Salesforce.

App Launcher