How to read and update a record with the value from a field in UI Builder?
Import
·
May 27, 2022
·
article
In my example, I will read and write a create a system property "date_format" in my application "UIB Examples" which scope is "x_snc_uib_examples". (If you're trying to change a Global system property it will require a separate discussion)
Let's create our own system property:
- Navigate to System Properties > UI Properties (sys_properties.list)
- Then create a new property and set the following values:
- 1. name = "date.format"
- scope= your application
- type = string
- value = dd-mm-yyyy
- read & write = Add the role snc_internal
- scope= your application
Once the property is created or you have a similar property or field to update, follow these steps:
- In UIB, Create a new data resource (db icon on the left > "Data resources" > (+ New) > Transform
- 1. this opens a new page
- Note, for non-system, we could use the out-of-the-box data resource "Look up record"
- 1. Name = getSystemProperty
- Description = Returns the system property
- Properties =
[{ "name":"propertyname", "label":"Property Name", "description":"Name of the system property", "readOnly":"false", "fieldType":"string", "mandatory":true,
"defaultValue":""}] - Script =
function transform(input) { if (!input.propertyName) return {"error" : "missing property name"}; var rec = new GlideRecord('sys_properties'); rec.addQuery('name', input.propertyName); rec.query(); if (rec.next()) { return { value:"" + rec.value sysId: rec.getUniqueValue() }; } else { return {"error": "property not found."}; }
} - Then click Submit
- Description = Returns the system property
- Note, for non-system, we could use the out-of-the-box data resource "Look up record"
- Go back to the UIB Page
- You should see the message "Data resources have changed outside of UIB"
- Click Update Page
- Add the new data resource to your page
- 1. go to Data resource instances (database icon on the left toolbar)
- in the first column, select your application
- select getSystemProperty
- Click Add
- You should see an error message "You don’t have permission to select this data resource. Contact your admin for more information". We'll fix that
- Open your getSystemProperty data resource
- 1. if you closed it, in the classic explorer, search for sys_ux_data_broker_transform.list
- Open "getSystemProperty"
- Click on the menu ☰
- Click on "Copy Sys id"
- Elevate your role to "security admin" (if you don't have the security admin role, ask the admin to create the ACL for you)
- in the classic explorer search for ACL, select Access Control (ACL)
- Click New and then set the following values:
- 1. Type: ux_data_broker
2. Operation: execute
3. Name: we copied earlier
4. Role: double-click and add the role of the app user or simply 'snc_internal' - Click Submit
- Open "getSystemProperty"
- Back to UIB
- in the first column, select your application
- You should see the result of the data broker = {"error": "missing property name"}
- It's normal we need to set the name of the property we need.
- in the data source, set property name to "x_snc_uib_examples.date_format"
- Your screen should look like
- Bind the data resource to the input field
- Select the input field
- then select the value field in the component's config panel on the right
- Select the database icon (dynamic data binding)
- Set the value to "@data.getsystemproperty.output.value"
- Your screen should look like
- Notice how the field is populated with the correct value. The reading of the record field is done. Let's move on to the saving part
See Part 2: How to update a record with the value from a field in UI Builder?
Labels:
View original source
https://www.servicenow.com/community/developer-articles/how-to-read-and-update-a-record-with-the-value-from-a-field-in/ta-p/2305654
