Auto-populating the values based on the other fields

2022_ServiceNow
Tera Expert

Hi everyone,

 

I have a requirement in the catalog item.

I have the data in the custom table, which is a string type and the display value to that field is false.

So, in the catalog form I have 3 fields. Same 3 fields are present in the custom table.

Field A - Location

Field B - Name

Field C - Other details

All the 3 fields are in the same custom table, and for field B, I have kept display value as 'True'. So it displays the options of name according to the location selected.

Now based on the names selected, I need to extract the data from Field C which is string and convert it to list view and display as options for Field C in the catalog form.

 

How can I achieve this? 

3 REPLIES 3

Sankar N
Kilo Guru

Please try with below script in client script, 

 

 

function onLoad() {
   var locationField = g_form.getControl('location_field');
   var nameField = g_form.getControl('name_field');
   var otherDetailsField = g_form.getControl('other_details_field');

   var locationValue = g_form.getValue('location_field');
   var nameValue = g_form.getValue('name_field');

   var gr = new GlideRecord('custom_table_name');
   gr.addQuery('location', locationValue);
   gr.addQuery('name', nameValue);
   gr.query();

   var options = [];
   while (gr.next()) {
      var otherDetails = gr.getValue('other_details');
      if (otherDetails) {
         options.push({'label': otherDetails, 'value': otherDetails});
      }
   }

   otherDetailsField.options.length = 0;
   otherDetailsField.options = options;
}

 

In the above code, replace "location_field", "name_field", "other_details_field", and "custom_table_name" with the actual field names and table name in your instance. With this code, when a user selects a value for Field A and Field B, the Client Script will query the custom table and fetch the values for Field C (Other Details), convert them to a list of options, and populate them in the Field C dropdown.

 

Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Syedmd08
Kilo Guru

 

To achieve this, you can create a Business Rule on the custom table that will run when the record is saved. The Business Rule will query the custom table for the selected name(s) and extract the data from Field C. Then, it will convert the data from Field C to a list view and store it in a variable.

Next, you can use a Client Script on the catalog form to populate the options for Field C based on the selected names in Field B. The Client Script will use GlideAjax to call a Script Include that will retrieve the data from the variable created by the Business Rule and populate the options for Field C.

Here are the steps to achieve this:

  1. Create a Business Rule on the custom table that will run when the record is saved. In the script section of the Business Rule, you can use GlideRecord queries to extract the data from Field C based on the selected names in Field B. Then, you can convert the data from Field C to a list view and store it in a variable. Here's some sample code:

 

var gr = new GlideRecord('custom_table');
gr.addQuery('name', 'IN', current.name.toString());
gr.query();

var options = [];
while (gr.next()) {
var data = gr.getValue('field_c');
if (data) {
options.push(data.split(','));
}
}

current.variable_name = options;

 

Note: replace 'custom_table' with the name of your custom table, and 'name', 'field_c', and 'variable_name' with the actual field names.

  1. Create a Script Include that will retrieve the data from the variable created by the Business Rule and convert it to an array. Here's some sample code:

var MyScriptInclude = Class.create();
MyScriptInclude.prototype = {
initialize: function() {
},

getOptions: function(names) {
var options = [];
var variable = gs.getProperty('my.variable.name');
var data = gs.getSession().getProperty(variable);
if (data) {
var array = data.split(',');
for (var i = 0; i < array.length; i++) {
options.push({'value': array[i], 'display': array[i]});
}
}
return options;
},

type: 'MyScriptInclude'
};

 

Note: replace 'my.variable.name' with the name of the variable you created in the Business Rule.

  1. Create a Client Script on the catalog form that will populate the options for Field C based on the selected names in Field B. In the Client Script, you can use GlideAjax to call the Script Include and retrieve the options. Then, you can populate the options for Field C using g_form.addOption(). Here's some sample code:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}

var ga = new GlideAjax('MyScriptInclude');
ga.addParam('sysparm_name', 'getOptions');
ga.addParam('sysparm_names', g_form.getValue('field_b'));
ga.getXML(populateOptions);
}

function populateOptions(response) {
var options = response.responseXML.getElementsByTagName('option');
var field = g_form.getControl('field_c');
g_form.clearOptions('field_c');
for (var i = 0; i < options.length; i++) {
var value = options[i].getAttribute('value');
var display = options[i].getAttribute('display');
g_form.addOption('field_c', value, display);
}
}

 

Note: replace 'MyScriptInclude' with the actual name of your Script Include, and 'field_b' and 'field_c' with actual field names

 

  1. Save all the scripts and test the catalog form. When a user selects a value in Field B, the options for Field C should be populated based on the selected name(s) and the data from Field C should be displayed as a list view.

Note: Make sure to test the Business Rule and Script Include separately before testing the catalog form to ensure that they are functioning correctly. Also, ensure that the Business Rule and Script Include are active and that the Client Script is attached to the correct catalog item.

 

 

 Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Pavankumar_1
Mega Patron

Hi @2022_ServiceNow ,

Try below onchange client script and script include to get the other details.

1. Onchange Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax("autoPopulateDetails"); //script include name
    ga.addParam("sysparm_name", "getDetails"); //function name
    ga.addParam("sysparm_sysid", newValue); //paramter pass to server
    ga.getXMLAnswer(setdetails); //callback funtion
    function setdetails(response) {
        g_form.setValue("otherdetailsname", response);//set the response on your variable
    }
}

2. Script include: check the Client callable is true.

var autoPopulateDetails = Class.create();
autoPopulateDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getDetails: function() {
        var val;
        var sysid = this.getParameter("sysparm_sysid");
        var grUsr = new GlideRecord("sys_user");//add your table name
        grUsr.addQuery("sys_id", sysid);//filters with sysid
        grUsr.query();
        if (grUsr.next()) {
            val = grUsr.location; //add your field name which you need to get
            return val;
        }
    },
    type: 'autoPopulateDetails'
});

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar