Autopopulate the field values.

venkata sai alu
Tera Contributor

I want to show the auto populated field value on form based the selected dropdown value,dropdown contains "n" number of values.dropdown values coming from the table and populated field value should also come from that table dropdown value record.

.

1 ACCEPTED SOLUTION

Deepak Shaerma
Kilo Sage

Hi @venkata sai alu 

You have to write Client Script and Script include to fetch the value from the tables. 
Step 1: Implement a Client Script to Populate the Field

Next, create a Client Script to listen for changes on the dropdown (Reference field) and populate your target field based on the selected value.

1. Navigate to the System Definition > Client Scripts.
2. Click New to create a new Client Script.
3. Set the Name, and choose the Table where your form belongs.
4. In the Script field, enter your JavaScript code. Here’s a simple example assuming your Reference field is named referenced_field, and you want to populate target_field with the description (or any relevant column) from the selected record on dropdown change:

 

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

    // Makes sure a value has been selected
    if (newValue) {
        // Perform a GlideAjax call to get the detail from the selected record
        var ga = new GlideAjax(‘YourScriptInclude’);
        ga.addParam(‘sysparm_name’, ‘getRecordDetail’);
        ga.addParam(‘sysparm_record_sys_id’, newValue); // Pass the sys_id of the selected record
        ga.getXMLAnswer(function(response){
            // Parse the response, and set the desired field’s value
            var result = response; // Assuming response is the value you want to set
            g_form.setValue(‘target_field’, result);
        });
    }
}

 


Step 2: Create a Script Include for Server-side Logic

Since Client Scripts run on the client side and cannot directly access server-side objects like GlideRecord for security reasons, you’ll use a GlideAjax call to interact with a Script Include.

1. Create a Script Include by navigating to System Definition > Script Includes.
2. Set the Name (use YourScriptInclude or another name as referenced in your Client Script), and ensure it’s Accessible from Client Scripts.
3. In the script field, add your server-side logic to fetch the desired record and return the relevant information. Example:

 

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

    getRecordDetail: function() {
        var recordSysId = this.getParameter(‘sysparm_record_sys_id’);
        var result = ‘’;

        // Assuming ‘your_table’ is the table where your records are
        var gr = new GlideRecord(‘your_table’);
        if(gr.get(recordSysId)) {
            // Example: Extracting the ‘description’ field
            result = gr.getValue(‘description’); 
        }
        return result;
    },

    type: ‘YourScriptInclude’
});

 


Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards 
Deepak Sharma

View solution in original post

4 REPLIES 4

dgarad
Giga Sage

Hi @venkata sai alu 

you can use the onChange client script for this.

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Service_RNow
Mega Sage

Hi @venkata sai alu follow the steps as mentioned in below thread

 

https://www.servicenow.com/community/developer-articles/solved-dependent-variable-on-catalog-item-us...

 

Please mark reply as Helpful/Correct, if applicable. Thanks!

Deepak Shaerma
Kilo Sage

Hi @venkata sai alu 

You have to write Client Script and Script include to fetch the value from the tables. 
Step 1: Implement a Client Script to Populate the Field

Next, create a Client Script to listen for changes on the dropdown (Reference field) and populate your target field based on the selected value.

1. Navigate to the System Definition > Client Scripts.
2. Click New to create a new Client Script.
3. Set the Name, and choose the Table where your form belongs.
4. In the Script field, enter your JavaScript code. Here’s a simple example assuming your Reference field is named referenced_field, and you want to populate target_field with the description (or any relevant column) from the selected record on dropdown change:

 

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

    // Makes sure a value has been selected
    if (newValue) {
        // Perform a GlideAjax call to get the detail from the selected record
        var ga = new GlideAjax(‘YourScriptInclude’);
        ga.addParam(‘sysparm_name’, ‘getRecordDetail’);
        ga.addParam(‘sysparm_record_sys_id’, newValue); // Pass the sys_id of the selected record
        ga.getXMLAnswer(function(response){
            // Parse the response, and set the desired field’s value
            var result = response; // Assuming response is the value you want to set
            g_form.setValue(‘target_field’, result);
        });
    }
}

 


Step 2: Create a Script Include for Server-side Logic

Since Client Scripts run on the client side and cannot directly access server-side objects like GlideRecord for security reasons, you’ll use a GlideAjax call to interact with a Script Include.

1. Create a Script Include by navigating to System Definition > Script Includes.
2. Set the Name (use YourScriptInclude or another name as referenced in your Client Script), and ensure it’s Accessible from Client Scripts.
3. In the script field, add your server-side logic to fetch the desired record and return the relevant information. Example:

 

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

    getRecordDetail: function() {
        var recordSysId = this.getParameter(‘sysparm_record_sys_id’);
        var result = ‘’;

        // Assuming ‘your_table’ is the table where your records are
        var gr = new GlideRecord(‘your_table’);
        if(gr.get(recordSysId)) {
            // Example: Extracting the ‘description’ field
            result = gr.getValue(‘description’); 
        }
        return result;
    },

    type: ‘YourScriptInclude’
});

 


Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards 
Deepak Sharma

Thank you.