autoppulation of fields

seshagirirao
Kilo Contributor

how to write client script and script include to autopopulate 2 fields at a time by selecting one field

 

4 REPLIES 4

abirakundu23
Mega Sage

 @seshagirirao,
Please follow my below answer : When select caller according to phone and email id  will be populated automatically.

Onchange Client script :

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


Var userDet = new GlideAjax ("TestScript"); //name of the script include
userDet.addParam('sysparam','userdetails'); // 'userdetails' method name

userDet.addParam('sysparam_sysid', newvalue); //select field
userDet.getXML(ajaxresponse);
function ajaxresponse(response)

 

{

var answer = response.responseXML.documentElement.getAttribute('answer');
var userdetails = JSON.Parse(answer);
g_form.setValue('caller_phone', userdetails.phone);
g_form.setvalue('caller_email',userdetails.email);

}
}
Script include:

"check" client callable option in Script Include.

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

    userdetails: function() {                               //called method

        var userID = this.getParameter('sysparm_userid');
        var grUser = new GlideRecord('sys_user');
        if (grUser.get(userID)) {
            var results = {                                                  // define JSON for user details
                "email": grUser.getValue("email"),
                "phone": grUser.getValue("phone")
            };
            return JSON.stringify(results);
        } else {
            return '';
        }
    },

    type: 'RequestorInfoUtils'
});


Please mark helpful and correct answer if it's worthy for you.

Hemanth M1
Giga Sage
Giga Sage

Hi @seshagirirao ,

 

Do you need really need script include(Any info needed from Server side)??? if its straight forward to set fields use below onChange client script.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
	//When Field changes and its not empty
    if (newValue) {
        g_form.setValue("Field_Name1", value1);
        g_form.setValue("Field_Name2", Value2);
    }

    //Type appropriate comment here, and begin script below

}

 

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

abirakundu23
Mega Sage

Hi @seshagirirao,
Did you get a chance to look my previous answer ?

Please mark helpful and correct answer if it's helpful for you and to assist others in the community as well.

Ankur Bawiskar
Tera Patron
Tera Patron

@seshagirirao 

it's an easy requirement which you can achieve using onChange + GlideAjax

what did you start with and where are you stuck?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader