Filling your own form from the chat

Follow a quote application from your page as the chat or the quote form fills it in, with the onApplicationUpdate callback and the application record it gives you, field by field.

When a visitor asks your AI agent for a quote, the agent starts an insurance application and fills it in as they talk: their name, their postcode, the answers to your questions, the cover they want, and later the price and the policy. Subcontext's own quote form follows the application live, so the visitor sees their answers appear in its fields.

Your page can do the same with its own form. Pass an onApplicationUpdate function to the webchat (or the quote form), and it is called with the whole application each time it gains or changes information. Copy what you need into your fields, a summary panel, or anything else on your page.

Adding the callback#

On the webchat:

js
SubcontextWebchat.init({
  // Your agent's id: in the dashboard, go to Agents and open the agent.
  agentId: 'YOUR_AGENT_ID',
  onApplicationUpdate(application, change) {
    document.querySelector('#first-name').value = application.customer.first_name || ''
    document.querySelector('#postcode').value = application.customer.postal_code || ''
    document.querySelector('#smoker').checked = application.risk_factors.smoker === true
  }
})

On the quote form, the same option takes the same function:

js
Subcontext.Quote.init({
  // ...container, key, productSlug, journeySlug...
  onApplicationUpdate(application, change) { /* the same record */ }
})

Both embeds build the record the same way, from the same live connection, so code written for one works with the other. If your page has both, pass the callback to one of them, or you'll get every update twice.

It's a JavaScript option only: a function can't go in a data-* attribute. If you install the webchat with the paste-in snippet, change to starting it from JavaScript to add one.

Webchat example 07 is a complete page: your own quote form, filled in as the visitor talks to the chat, with a log of each change.

When it's called#

First, with everything so far. The first call comes as soon as the embed is following an application, with every field known at that point, and change.initial set to true. For the webchat, that is:

  • when the agent starts an application in the conversation,
  • when a Subcontext quote form on the page links its application to the chat,
  • when a visitor comes back to a conversation that already has an application, for example after reloading the page. The chat picks the conversation up when the visitor opens it, or when your page calls startChat(), and the first call comes then.

Then, after each change: the visitor answers a question in the chat, the quote form saves a page, the agent gives a price, the policy is bought. An update that changes nothing (the quote form saving an answer it already had, say) isn't passed on.

A new application starts again. If the agent starts another application, the quote form's "Start a new application" link is used, or you call resetConversation(), the next call is for the new application, with a new id and change.initial set to true. After resetConversation() on the webchat, nothing is called until there is a new application.

If your function throws an error, the embed logs it in the browser console and carries on. The next update still calls it.

The callback's arguments#

js
onApplicationUpdate(application, change)
  • application is the application record: the whole application so far, not only what changed. It's your own copy, so you can change it freely.
  • change says what this update changed:
Field Type What it holds
changed list of text The fields that changed, in alphabetical order. A field in one of the record's groups is named with its group, like customer.first_name or risk_factors.smoker; the others by their own name, like status, cover_amount_pence or quote. On the first call, every field that has a value.
source 'agent', 'form' or null Who made the change: 'agent' is the chat's agent; 'form' is a quote form, or your own code using the API. null on the first call.
initial true or false true for the first call for an application.

Use changed to update only the fields that changed, or to highlight them. To skip a field the visitor is typing in, check document.activeElement before you write to it.

The application record#

The field names are the API's own, as in Get an application. A field with nothing in it yet is null, an empty object or false, as shown below; it's never left out.

Field Type What it holds
id text The application's id. The same id the API and your dashboard use.
status text Where the application has got to. See Statuses.
customer object The person applying. See People. {} until something is known.
joint_customer object or null The second person on a joint application, once they're added. See People.
risk_factors object The answers so far, keyed by the journey's question keys (see Get a journey's questions). Each answer is text, a number, true or false, or a list of text, as its question asks.
cover_amount_pence number or null The amount of cover, in pence: 15000000 is £150,000.
term_years number or null How long the cover lasts, in years.
interstitial_choices object The option chosen on each choice page ("Just life cover, or critical illness too?"), keyed by the page.
reflections object The visitor's answers in their own words, keyed by the page that asked. An empty text means they skipped it.
town text or null The town found for the visitor's postcode.
health_data_consent true or false Whether the visitor has agreed to their health details being used.
quote object or null The latest price. See Quote.
policy object or null The policy, once it's bought. See Policy.

Statuses#

status Meaning
anonymous Started, but nothing yet says who the visitor is.
started, in_capture Being filled in.
quoted Priced: quote has the price.
referred Sent to an underwriter to decide.
declined, postponed Underwriting can't offer cover, or can't yet.
handed_off Passed to someone else to finish: your team, or another provider.
bound Bought: policy has the policy.
abandoned Given up on.

People#

customer and joint_customer have these fields. A field is only there once it's known. joint_customer has no address: the second person shares the first's.

Field Type What it holds
title text Mr, Ms, Dr and so on.
first_name text
last_name text
email text
phone_number text
dob text Date of birth, as YYYY-MM-DD.
address_line1 text
address_line2 text
city text
postal_code text The postcode.
country text

Quote#

Field Type What it holds
id text The quote's id.
reference text The quote's reference, as the visitor sees it.
status text The quote's own status: issued, accepted, superseded, expired or rejected.
overall_decision text or null Underwriting's decision: accept_standard, accept_rated (at a higher price) or mixed (different for different covers).
currency_code text GBP.
total_premium_monthly_pence number or null The monthly price, in pence.
policy_fee_pence number or null A fee added to the price, if the product has one.
bundle_discount_pence number or null A discount for buying covers together, if there is one.
expires_at text or null When the price ends, as an ISO 8601 time.
components list One for each cover in the quote: cover_type, title, sum_assured_pence, term_years, rating_pct (any loading, as a percentage), member_count (on a group plan) and premium_monthly_pence. A value not set is null.

Policy#

Field Type What it holds
id text The policy's id.
policy_number text The number the customer sees.
commencement_date text When cover starts, as YYYY-MM-DD.
total_premium_monthly_pence number or null The monthly price, in pence.
currency_code text GBP.
first_collection_on text or null When the first Direct Debit payment is taken, as YYYY-MM-DD.
collection_day_of_month number or null The day of the month payments are taken.
policy_hub_path text or null The full address where the customer signs in to manage their policy.
schedule_path text or null The full address of the policy schedule.
documents list The policy's documents: id, kind, name and path (the full address).

Keeping it private#

The record holds the visitor's personal details and their answers, which can include their health. It goes only to the function you pass: the embeds don't send it as a page event, so other scripts on your page (analytics, ad tags, chat or survey tools) don't see it. Don't pass it on to them, or keep it in the browser's storage.

It's the visitor's own information, which they gave on your website, and it goes only to their browser: the webchat follows only the application its own conversation is linked to, and the quote form only the one it started or was given by the chat.

Nothing changes for your content security policy: the webchat uses the connection it already has (wss://subcontext.com).

Stuck, or something missing?

Get in touch and a person on the team will help.

Contact us