Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

sushmapinapati
Tera Contributor

ATF -  Custom Step Configuration - Get Display value of a Field 

Hi,

As part of my ATF test development, I ran into cases where I need the display value of certain fields in the record generated. I have created an Custom step - server independent that retrieves the display of a field and passes to output variable.

Here is the information on creating this step configuration:

Step Environment: Server Independent

Category: Server

Input Variables:

  • Input String: Column Name = u_tablename, Type = String
  • Input String: Column Name = u_sysid, Type = String
  • Input String: Column Name = u_fieldname, Type = String

Output Variables:

  • Output String: Column Name = u_displayvalue, Type = String

Description Script:

function generateDescription(step) {
// the global variable 'step' represents the current glide record
var description = gs.getMessage("Retrieves the display value of field : '{0}'",
step.inputs.u_fieldname);
return description;
}
generateDescription(step);

Step execution script:

(function executeStep(inputs, outputs, stepResult, timeout) {
var tableName = inputs.u_tablename;
var sysId = inputs.u_sysid;
var fieldName = inputs.u_fieldname;
var rowCount;
var displayName;

var MESSAGE_KEY_NO_DISPLAY_VALUE = "Failure: Display name cannot be retrieved, either the Tablename/FieldName are not correct or Field does not have any value";
var MESSAGE_KEY_SUCCESS = "Success: Display value of the field " + fieldName +" is passed to the output variable";

function logic() {

var gr = new GlideRecord(tableName);
gr.addQuery('sys_id', sysId);
gr.query();
rowCount = gr.getRowCount();
while(gr.next()){
displayName = gr.getDisplayValue(fieldName);
}
}

function passStep() {
outputs.u_displayvalue = displayName;
stepResult.setOutputMessage(MESSAGE_KEY_SUCCESS);
stepResult.setSuccess();

}

function failStep() {
stepResult.setOutputMessage(MESSAGE_KEY_NO_DISPLAY_VALUE);
stepResult.setFailed();

}

logic();
if (rowCount >= 1 && displayName !== null) {
passStep();
}
else {
failStep();
}
}(inputs, outputs, stepResult, timeout));

 

Comments
Not applicable

Thanks a lot, sushmapinamati. This works like a charm.

I am unknown
Tera Contributor

if var fieldName = inputs.u_fieldname;

 

//My Query -if fieldName is Apple123 and Inputs.U_fieldname is only Apple 

then what operator should we use to validate

felixmiske
Tera Guru

Hi @sushmapinapati - thank you for your work. It really helped me out.

 

I took your work, adapted it a bit since it did fail for me - and wrapped up the resulting Test Step as an Update Set: TestStep_AssertFieldDisplayValue_FM.

 

The configuration in detail:

 

Name: Assert Field Display Value
Step environment: Server - REST (Server - Independent did not work for me, unsure why)

Category: REST

Batch order constraint: Start a batch execution
Class type: Script
 
Template reminder: Assert that the field’s display value matches the expected string, e.g. “Assert the Application Menu title is ‘Fuhrpark’”.

HTML description:
```html

<p>

  <strong>Assert Field Display Value</strong> <br>

  <span style="color:#666;font-size:90%;">

    This test step <b>verifies</b> that the <span style="color:#205081;">display value</span> of a specified field on a record matches the <b>expected string</b>.<br>

    The test will <span style="color:#b72a2a;">fail</span> if the table, field, or record cannot be found, or if the actual display value does not match your expectation.<br>

  </span>

  <ul>

    <li><b>Table Name</b>: Use the Table picker to select the table (e.g., <code>incident</code>).</li>

    <li><b>Record Sys ID</b>: Paste or query the record's sys_id to target a specific row.</li>

    <li><b>Field Name</b>: Enter the technical field name (e.g., <code>short_description</code>, <code>title</code>).</li>

    <li><b>Expected Display Value</b>: Enter the exact string you expect (including translation if relevant).</li>

  </ul>

  <span style="font-size:90%;color:#888;">

    <em>Pro tip:</em> This step is useful for validating multi-language values and reference/choice field labels.

  </span>

</p>

```


Description generation script:
```js
function generateDescription(step) {
return gs.getMessage(
"Assert display value of field '{0}' on table '{1}' (sys_id: {2}) is '{3}'",
[
step.inputs.u_field_name,
step.inputs.u_table_name,
step.inputs.u_record_sys_id,
step.inputs.u_expected_display_value
]
);
}
generateDescription(step);
```

Step execution script:
```js

(function executeStep(inputs, outputs, stepResult, timeout) {
var tableName = inputs.u_table_name && inputs.u_table_name.trim();
var recordSysId = inputs.u_record_sys_id && inputs.u_record_sys_id.trim();
var fieldName = inputs.u_field_name && inputs.u_field_name.trim();
var expectedDisplayValue = inputs.u_expected_display_value;

var MESSAGE_FAIL_NO_TABLE = "Failure: Table '" + tableName + "' does not exist or is not accessible.";
var MESSAGE_FAIL_NO_FIELD = "Failure: Field '" + fieldName + "' does not exist on table '" + tableName + "'.";
var MESSAGE_FAIL_NO_RECORD = "Failure: Record not found for sys_id '" + recordSysId + "' in table '" + tableName + "'.";
var MESSAGE_FAIL_NO_DISPLAY = "Failure: Display value cannot be retrieved; table/field may be invalid or field is empty.";
var MESSAGE_FAIL_MISMATCH = "Failure: Display value mismatch. Expected: '" + expectedDisplayValue + "'";
var MESSAGE_SUCCESS = "Success: Display value matches expected value: '" + expectedDisplayValue + "'";

// --- Robust comparison function ---
function robustCompare(a, b) {
function prep(str) {
if (!str) return '';
// Normalize Unicode (NFKC), trim, and collapse whitespace
return str.normalize("NFKC").replace(/\s+/g, ' ').trim();
// For case-insensitive comparison, use:
// return str.normalize("NFKC").replace(/\s+/g, ' ').trim().toLowerCase();
}
return prep(a) === prep(b);
}

try {
// Input validation
if (!tableName || !recordSysId || !fieldName || expectedDisplayValue === undefined) {
stepResult.setOutputMessage("Failure: One or more required inputs are missing.");
stepResult.setFailed();
return;
}

var td = GlideTableDescriptor.get(tableName);
if (!td.isValid()) {
stepResult.setOutputMessage(MESSAGE_FAIL_NO_TABLE);
stepResult.setFailed();
return;
}

var gr = new GlideRecord(tableName);
if (!gr.isValidField(fieldName)) {
stepResult.setOutputMessage(MESSAGE_FAIL_NO_FIELD);
stepResult.setFailed();
return;
}

if (!gr.get(recordSysId)) {
stepResult.setOutputMessage(MESSAGE_FAIL_NO_RECORD);
stepResult.setFailed();
return;
}

var displayValue = gr.getDisplayValue(fieldName);
outputs.u_display_value = displayValue; // Optional, for chaining in other steps

if (displayValue === null || displayValue === "") {
stepResult.setOutputMessage(MESSAGE_FAIL_NO_DISPLAY);
stepResult.setFailed();
return;
}

if (!robustCompare(displayValue, expectedDisplayValue)) {
stepResult.setOutputMessage(
MESSAGE_FAIL_MISMATCH + ". Actual: '" + displayValue + "'"
);
stepResult.setFailed();
return;
}

stepResult.setOutputMessage(MESSAGE_SUCCESS);
stepResult.setSuccess();

} catch (e) {
stepResult.setOutputMessage("Unexpected error: " + e.message);
stepResult.setFailed();
}
})(inputs, outputs, stepResult, timeout);

```

Input Variables: 
NameLabelTypeOrder

u_table_name

Table Name

Table Name

100

u_record_sys_id

Record Sys ID

String

200

u_field_name

Field Name

String

300

u_expected_display_value

Expected Display Value

String

400



Output Variable (optional for debugging/logging):

u_display_value

Display Value

String

Version history
Last update:
‎10-26-2021 04:05 AM
Updated by: