Auto Populate record producer field on the selection of first field.

Kruthik M Shiva
Tera Contributor

Hi All,
I have a requirement like the record producer fields should be auto filled based on the first two field selection.
E.g. I have to two field business function and business process so once if user select those fields and if already there is a record existed with same business function and business process in the table then all the remaining fields should be auto populated same as already existing record how to do this if anyone has pointers and scripts for this, please do share.
Thanks in advance.

1 ACCEPTED SOLUTION

Hi @Kruthik M Shiva,

Hope you are doing well and Glad to see that you are following my answers and solutions and happy to assist you. 

 

Proposed Solution

As a solution, you need to modify above script as same as mentioned below and I tried it on my Personal Developer Instance to get the desired outputs. Hope this will work for you now and let me know if any other help needed. Attaching screenshots for the reference.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (g_form.getValue('u_category') != '') {
        if (newValue != '') {
            var val = confirm('If you want to copy same fields?');
            if (val) {
                var ga = new GlideAjax('global.AutoPopulateIncidentDetails');
                ga.addParam('sysparm_name', 'incidentDetails');
                ga.addParam('sysparm_category', g_form.getValue('u_category'));
                ga.addParam('sysparm_sub_category', newValue);
                ga.getXML(populateDetails);

                function populateDetails(response) {
                    var answer = response.responseXML.documentElement.getAttribute("answer");
                    var ans = JSON.parse(answer);
                    g_form.setValue('short_description', ans[0]);
                    g_form.setValue('state', ans[1]);
                }
            }
			else{
				alert('You cancel the Auto Population Fields Request.');
			}
        }
    } else {
        alert('Kindly fill category first.');
        g_form.setValue('u_sub_category', '');
    }
}

Sample Outputs: -

AakashG2703_0-1712213443456.png

After Pressing Ok: - Fields will be auto populated.

AakashG2703_1-1712213518202.png

After Pressing Cancel: - Field will not be auto populate and 1 alert message will be displayed.

AakashG2703_2-1712213610274.png

 

 

If you find this information/knowledge/solution helpful, please don't forget to mark my solution and reply as helpful and accepted.

 

Thanks ‌‌:)

Aakash Garg

ServiceNow Developer

View solution in original post

8 REPLIES 8

Abhijeet_Pawar
Tera Guru

Hello @Kruthik M Shiva ,

write onchange client on both fields.

please refer below script for your reference.

Thanks.

 

Script include:

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

    getExistingRecordData: function(businessFunction, businessProcess) {
        var existingRecordData = {};

        var existingRecord = new GlideRecord('your_table_name');
        existingRecord.addQuery('business_function', businessFunction);
        existingRecord.addQuery('business_process', businessProcess);
        existingRecord.query();

        if (existingRecord.next()) {
        
            existingRecordData.field1 = existingRecord.getValue('field1');
            existingRecordData.field2 = existingRecord.getValue('field2');
           
        }

        return existingRecordData;
    }

});

Onchange client for both fields.

var businessFunction = g_form.getValue('business_function');
    var businessProcess = g_form.getValue('business_process');

    if (businessFunction && businessProcess) {
        var ga = new GlideAjax('AutoFillFieldsScriptInclude');
        ga.addParam('sysparm_name', 'getExistingRecordData');
        ga.addParam('business_function', businessFunction);
        ga.addParam('business_process', businessProcess);
        ga.getXMLAnswer(function(response) {
            if (response) {
                var existingRecordData = JSON.parse(response);
           
                g_form.setValue('field_to_auto_populate1', existingRecordData.field1);
                g_form.setValue('field_to_auto_populate2', existingRecordData.field2);
              
            }
        });
    }

 

 

AakashG2703
Mega Guru

Hi @Kruthik M Shiva, 

Hope you are doing well.

 

Proposed Solution

As a solution, I tried to implement your query or ask on my Personal Developer Instance to get the solution as soon as possible. For this, I refer to the "Incident" as a Table and "Category as Business Function" and SubCategory as Business Process" and tried to auto populate the value of "State and Short Description" Field on the "Record Producer" if any existing record found with the help of an "On-Change Catalog Client Script and Script Include". Kindly follow below scripts : -

 

Client Script: -

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (g_form.getValue('u_category') != '') {
        var ga = new GlideAjax('global.AutoPopulateIncidentDetails');
        ga.addParam('sysparm_name', 'incidentDetails');
        ga.addParam('sysparm_category', g_form.getValue('u_category'));
        ga.addParam('sysparm_sub_category', newValue);
        ga.getXML(populateDetails);

        function populateDetails(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
			var ans = JSON.parse(answer);
			g_form.setValue('short_description', ans[0]);
			g_form.setValue('state', ans[1]);
        }
    } else {
        alert('Kindly fill category first.');
		g_form.setValue('u_sub_category', '');
    }
}

 

Script Include: -

var AutoPopulateIncidentDetails = Class.create();
AutoPopulateIncidentDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    incidentDetails: function() {
		var arr = [];
        var category = this.getParameter('sysparm_category');
		var subCategory = this.getParameter('sysparm_sub_category');
		var gr = new GlideRecord("incident");
		gr.addQuery("category", category);
		gr.addQuery("subcategory", subCategory);
		gr.query();
		if (gr.next()) {
			arr.push(gr.short_description.toString());
			arr.push(gr.state.toString());
		}
		return JSON.stringify(arr);
    },
    type: 'AutoPopulateIncidentDetails'
});

 

For your reference, also attaching screenshots of the outputs that will give you better insights of how the scripts are working or the best thing will be to follow the solution and execute the scripts on your instance.

 

Client Script

AakashG2703_0-1712147718281.png

Script Include

AakashG2703_1-1712147746044.png

Record Producer

AakashG2703_2-1712147795600.png

 

AakashG2703_3-1712147893871.png

 

If you find this information/knowledge/solution helpful, please don't forget to mark my solution and reply as helpful and accepted.

 

Thanks ‌‌:)

Aakash Garg

ServiceNow Developer

Hi @AakashG2703 and @Abhijeet_Pawar Thanks for giving your time and quick response I will implement the same and accept solution once it is done, I will accept your solution.
Thanks&Regards,
Kruthik Shivaprasad

Glad to hear that @Kruthik M Shiva. Hope our solutions will resolve your ask or query and let me know if any help needed.

 

If you find any information/knowledge/solution helpful, please don't forget to mark my solution and reply as helpful and accepted.

 

Thanks ‌‌:)

Aakash Garg

ServiceNow Developer