autopopulate field depending on another field

BenjaminY
Tera Contributor

Hello,

I am tasked to fix a bug on a client script that when the field "please_provide_a_number" is inputted, the field "a_number" should be auto populated with the same input. I created a client script, but I believe it is not triggering due to Initial script. Should i add a default value on the table to autopopulate the field from the app studio? If so what would be the best way of going about this. Below is scripts:

initial script: 

variable name: please_provide_a_number

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

    //Type appropriate comment here, and begin script below

        //Type appropriate comment here, and begin script below
        var recordNum = g_form.getValue('please_provide_a_number');
        var ajax = new GlideAjax('alienNumCCHT');
        ajax.addParam('sysparm_name', 'alienNumCCHT');
        ajax.addParam('sysparm_record_num', recordNum);
        ajax.getXMLAnswer(populateCpRecord);

        function populateCpRecord(response){
            var ans = JSON.parse(response);
            g_form.setValue('extention_victim_last_name', ans.lastName);
            g_form.setValue('extention_victim_first_name', ans.firstName);
            g_form.setValue('extention_victim_middle_name', ans.middleName);
            g_form.setValue('address_line_1', ans.addressOne);
            g_form.setValue('ext_victim_address_line_2', ans.addressTwo);
            g_form.setValue('extention_victim_alies', ans.alias);
            g_form.setValue('ext_victim_city', ans.city);
            g_form.setValue('ext_victim_state', ans.state);
            g_form.setValue('ext_social_security_number', ans.social);
            g_form.setValue('ext_date_of_birth', ans.dob);
            g_form.setValue('ext_country_of_citizenship', ans.coc);
            g_form.setValue('ext_country_of_birth', ans.cob);
            g_form.setValue('ext_gender', ans.gender);
            g_form.setValue('lead_case_last_name', ans.leadLast);
            g_form.setValue('lead_case_email_address', ans.leadEmail);
            g_form.setValue('lead_case_first_name', ans.leadFirst);
            g_form.setValue('lead_case_phone_number', ans.leadNum);
            g_form.setValue('lead_case_middle_name', ans.leadMiddle);
            g_form.setValue('lead_case_ext', ans.leadExt);
            g_form.setValue('wc_ext_last_name', ans.vwLast);
            g_form.setValue('wc_ext_first_name', ans.vwFirst);
            g_form.setValue('wc_ext_middle_name', ans.vwMiddle);
            g_form.setValue('wc_ext_phone_number', ans.vwNum);
            g_form.setValue('wc_ext_email', ans.vwEmail);
            g_form.setValue('vw_ext_officer', ans.vwExt);
            g_form.setValue('ext_case_agent_last_name', ans.mLast);
            g_form.setValue('ext_case_agent_last_name_first_name', ans.mFirst);
            g_form.setValue('ext_case_agent_middle_name', ans.mMiddle);
            g_form.setValue('ext_case_agent_phone_number', ans.mNum);
            g_form.setValue('ext_case_email', ans.mEmail);
            g_form.setValue('ext_lead_case', ans.mExt);
            g_form.setValue('a_number', ans.aNum);
            g_form.setValue('lead_case_agency_office', ans.lea);
            g_form.setValue('ead_expiration_date', ans.exp);
            // alert("CP Record Num is " + ans);
        }
}

second script to check if a_number field has been filled, if not populate...
variable name: please_provide_a_number
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    // Get input from "please_provide_a_number" field
    var numberValue = g_form.getValue('please_provide_a_number');

    // Only populate the "a_number" field if not already populated (to avoid overwriting other client scirpt)
    if (!g_form.getValue('a_number')) {
        g_form.setValue('a_number', numberValue);
    }
2 REPLIES 2

Zach Koch
Giga Sage
Giga Sage

My guess, without seeing your Script Include script from your Ajax call, is that you are having a script order issue. You have two onChange client scripts running onChange on the same field. So you don't know which one will run first unless one has a higher order to run later. Can you post a screenshot of your script include method you are calling, the one named ,alienNumCCHT?

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

Brad Bowman
Kilo Patron
Kilo Patron

Instead of a second onChange script on the same field, just replace the setValue line for 'a_number' in the 'initial script':

if (ans.aNum) {
    g_form.setValue('a_number', ans.aNum);
} else {
    g_form.setValue('a_number', newValue);
}