logo

NJP

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:

  1. Navigate to System Properties > UI Properties (sys_properties.list)
  2. Then create a new property and set the following values:
  3. 1. name = "date.format"
    1. scope= your application
    2. type = string
    3. value = dd-mm-yyyy
    4. read & write = Add the role snc_internal

Once the property is created or you have a similar property or field to update, follow these steps:

  1. In UIB, Create a new data resource (db icon on the left > "Data resources" > (+ New) > Transform
  2. 1. this opens a new page
    1. Note, for non-system, we could use the out-of-the-box data resource "Look up record"
    2. 1. Name = getSystemProperty
      1. Description = Returns the system property
      2. Properties =
        [{ "name":"propertyname",    "label":"Property Name",  "description":"Name of the system property",  "readOnly":"false",  "fieldType":"string",  "mandatory":true,
          "defaultValue":""}]
      3. 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."}; }
        }
      4. Then click Submit
  3. Go back to the UIB Page
  4. You should see the message "Data resources have changed outside of UIB"
  5. Click Update Page
  6. Add the new data resource to your page
  7. 1. go to Data resource instances (database icon on the left toolbar)
    1. in the first column, select your application
    2. select getSystemProperty
    3. Click Add
    4. 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
    5. Open your getSystemProperty data resource
    6. 1. if you closed it, in the classic explorer, search for sys_ux_data_broker_transform.list
      1. Open "getSystemProperty"
      2. Click on the menu ☰
      3. Click on "Copy Sys id"
      4. Elevate your role to "security admin" (if you don't have the security admin role, ask the admin to create the ACL for you)
      5. in the classic explorer search for ACL, select Access Control (ACL)
      6. Click New and then set the following values:
      7. 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'
      8. Click Submit
    7. Back to UIB
  8. You should see the result of the data broker = {"error": "missing property name"}
  9. It's normal we need to set the name of the property we need.
  10. in the data source, set property name to "x_snc_uib_examples.date_format"
  11. Your screen should look like
    image
  12. Bind the data resource to the input field
  13. Select the input field
  14. then select the value field in the component's config panel on the right
  15. Select the database icon (dynamic data binding)
  16. Set the value to "@data.getsystemproperty.output.value"
  17. Your screen should look like
  18. image
  19. 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:

image

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