The CreatorCon Call for Content is officially open! Get started here.

UI Builder - how to check for existing, matching record before save, and give user option to cancel?

thomaskennedy
Tera Guru

I've been following the video on constructing a simple app and inserting records. My button does an insert, or an update, like so:

const macAddress = api.state.deviceMacAddress || "";
const serialNumber = api.state.deviceSerialNumber || "";
const model = api.state.deviceModel || "";
...

var query = `u_mac_address=${macAddress}`;
query += `^u_model=${model}`;
query += `^u_serial_number=${serialNumber}`;
...

var payload = {
"table": api.context.props.table,
"recordId":api.context.props.sysId,
"templateFields":query,
"useSetDisplayValue":false
};

api.data.update_device.execute(payload);

 

And that works fine. But I'd like to check (through a Look Up Records resource) if there is an existing record matching these values, and give the user the option to skip the insert (or update), such as by displaying a modal that gives the user the option to cancel. But I do not see how to launch a modal from here, or take any action that informs the user or gives them an option to cancel.

 

I feel like I need to emit an event from the save button, and assign a handler that checks the data resource, and if it sees an existing record, it emits Event A, otherwise event B, and the handler for A pops up a modal, while the handler for B proceeds with the insert/update. Is there a way to do this?

1 REPLY 1

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I think what I would do here is a series of things chained together:

  1. On the button press, call/refresh the lookup data resource (it'll execute async).
  2. The data resource should fire an event when it successfully completes, and you can run a client script then that examines the payload and either does the update or opens the modal.
  3. If the user doesn't want to cancel, call the client script that executes your update device data resource.