How to update a record with the value from a field in UI Builder?
Import
·
May 27, 2022
·
article
- Go to Client state parameters (card icon on the left toolbar)
- Click +Add
- Set N*ame* to "date_format"
- Set Initial value to "@data.getsystemproperty.output.value"
- Select the Input field
- On the config panel on the right, go to the Events tab
- In the section "Input value set", Click on "+Add a new event handler"
- Search for "update", then select Update client state parameter
- For "Client State Parameter Name", select "date_format"
- For "New Value", click on the "dynamic data binding" icon and type "@payload.value"
- Click Add.
- Click Save
- Go to Page Scripts ( icon </> on the left toolbar)
- Click +Add
- Set Script name to a meaningful name. Ex: Update Property
- For now, to show how the different pieces connect together quickly, we'll simply print a message, so add the following snippet n the code editor:
function handler({api, event, helpers, imports}) { console.log("Update Property Script"); console.log("date_format = " + api.state.date_format);
} - Notice how the call to "api.state. gives access to our page variables
- Select the "Save" button
- Go to the config panel on the right
- Click on the Events tab
- Click on "+Add a new event handler"
- in the new dialog, in the "</> Script" section, select "Update Property"
- Click Add
- Save your UIB Page
- Click on the Open button to preview the page
- In the preview page, update the field value (ex: dd-mm-yy)
- then Click on the Save button
- You will see the value printed in the console. All the required elements are connected. We now need to make the update permanent by
- 1. adding an "update record" data resource
- and calling it with the new value stored in the page variable
- Go to Data Resources ( icon database on the left toolbar)
- Click +Add
- Search for "Update record"
- Select "Update record"
- Click Add
- Update the Page script with the following code:
function handler({api,event, helpers, imports}) { let fields = "value=" + api.state.date_format; api.data.update_record_1.execute({ table: "sys_properties", recordId : api.data.getsystemproperty.output.sysId, templateFields: fields, useSetDisplayValue: false });
} - Save the page
- Click on the Open button to preview the page
- In the preview page, update the field value (ex: yyyy-mm-dd)
- Click on the Save button
- Check your record, and voila it was updated.
Labels:
View original source
https://www.servicenow.com/community/next-experience-articles/how-to-update-a-record-with-the-value-from-a-field-in-ui-builder/ta-p/2331937
