Is it possible to automatically clear a field when the data look-up returns nothing?

C_dric Chen
Tera Contributor

Hello, people.

 

I have a few fields on a record producer who have their catalog data look-up set up. When the data look-up returns something, SN is populating these fields as expected. However, when the data look-up doesn't return anything, SN does not clear these fields, but I need SN to clear these fields.

 

My co-worker and I thought about using catalog client scripts, but we're concerned that we don't know which component would run first: the data look-up that retrieve data or the client script that clears fields. If the client script runs after the data look-up, it would defeat the purpose of the data look-up.

 

Would anyone kindly point me at the right direction to making SN clear the fields when the data look-up does not return anything?

 

Thank you in advance!

2 REPLIES 2

Amit Gujarathi
Giga Sage
Giga Sage

HI @C_dric Chen ,
I trust you are doing great.

  1. Open the record producer form in the ServiceNow Designer.

  2. Identify the fields that are populated by the data lookup and need to be cleared when no data is returned.

  3. Add a client script to the record producer that runs after the data lookup. This script will check if the data lookup has returned any values and clear the fields if necessary.

Here's an example of how the client script can be implemented:

 

function onLoad() {
  // Check if the data lookup field has any value
  var lookupValue = g_form.getValue('lookup_field'); // Replace 'lookup_field' with the actual field name

  // If the lookup value is empty, clear the dependent fields
  if (lookupValue === '') {
    g_form.clearValue('dependent_field1'); // Replace 'dependent_field1' with the actual field name
    g_form.clearValue('dependent_field2'); // Replace 'dependent_field2' with the actual field name
    // Add more dependent fields as necessary
  }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hello.

 

Is this a client script or a catalog client script? If it's a catalog client script, how do I make it look up data from a table, but not the record producer itself?

 

Thanks.