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

Hi @AakashG2703 ,
Just wanted to add one extra thing that if business function and business process matches it should ask for confirmation message like if you want to copy same fields if they clicked ok then only it should auto populate else it should not be. Could you please help me what changes needs to be done for this?
Thanks,
Kruthik Shivaprasad

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

Hi @AakashG2703 ,
I tried with your given code but it not working for me. In my case business function is choice field and process are text field though I have given the same text it didn't worked for me.
Script Include:

var AutoPopulateROPDetails = Class.create();
AutoPopulateROPDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    ROPDetails: function() {
        var arr = [];
        var category = this.getParameter('sysparm_category');
        var subCategory = this.getParameter('sysparm_sub_category');
        var gr = new GlideRecord("x_records_of_rop");
        gr.addQuery("business_function", category);
        gr.addQuery("process", subCategory);
        gr.query();
        if (gr.next()) {
            arr.push(gr.sub_process.toString());
            arr.push(gr.description_of_process_procedure.toString());
        }
        return JSON.stringify(arr);
    },
    type: 'AutoPopulateROPDetails'
});
Catalog client script:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (g_form.getValue('business_function') != '') {
        if (newValue != '') {
            var val = confirm('If you want to copy same fields?');
            if (val) {
                var ga = new GlideAjax('global.AutoPopulateROPDetails');
                ga.addParam('sysparm_name', 'ROPDetails');
                ga.addParam('sysparm_category', g_form.getValue('business_function'));
                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('sub_process', ans[0]);
                    g_form.setValue('description_of_process_procedure', ans[1]);
                }
            } else {
                alert('You cancel the Auto Population Fields Request.');
            }
        }
    }
}
Could you please help me what is the issue why it is not working.
Thanks,
Kruthik Shivaprasad

Hi @Kruthik M Shiva,

 

Kindly try to debug your code by using an "alert()" function or method in your client script like: -

  1. Check whether you are able to get the value of Business Function and Business Process using alert().
  2. Further check whether you are able to make a right query and getting the data or not in an array. Try to use gs.log().
  3. Further check whether you are able to receive any value from Script Include or not by using an alert().

These are some point that will be helpful for you to debug your code mentioned above. Let me know if you still need some help and try to provide some screenshots of the output by using above statements so that I will be able to rectify the issue or challenge in scripting or what we are missing.

 

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