How to auto populate string field value to the reference type variable?

Ashwini Jadhao
Giga Guru

Hi Community,

So on the catalog form I have the "software_id " reference variable which is referring to "profile_table" and  I am trying to auto populate the  "server_collation" string field's value  which is present in the table named as "profile_Table" to the "serverCollation " reference type variable which is referring to "collation table".

So for this I have written the following client script on catalog form.

function onChange(control, oldValue, newValue, isLoading) {

var software_id = g_form.getReference('software_id ', collationName);

function collationName(software_id) {
var s_collation = g_form.setValue('serverCollation ', software_id.server_collation);
alert('s_collation' + s_collation);
//var database_collation = g_form.setValue('database_collation', s_collation);
//alert('database_collation' + database_collation);
}
}?

 Could you please help in this script logic?

1 ACCEPTED SOLUTION

Hi,

If the field which you are trying to set is of type reference then setting sys_id is fine

But if the type is string then setting sys_id for string variable won't work.

refer example below for GlideAjax

Script Include: It should be client callable

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getDetail: function(){

        var id = this.getParameter('sysparm_profileID');            

        var gr = new GlideRecord('profile_table');
        gr.addQuery('sys_id', id);
        gr.query();
        if(gr.next()){
            return gr.server_collation.getDisplayValue();
        }
        return '';
    },
    
    type: 'checkRecords'
});

Client script: onchange of software id

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	
    var ga = new GlideAjax('checkRecords');
    ga.addParam('sysparm_name', "getDetail");
    ga.addParam('sysparm_profileID', g_form.getValue('software_id'));
    ga.getXMLAnswer(function(answer){
        if(answer != ''){
           g_form.setValue('database_collation', answer);
        }
    });

}

Regards
Ankur

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

View solution in original post

5 REPLIES 5

Willem
Giga Sage
Giga Sage

Can you try calling a Client callable script include using Glide Ajax.

 

Client script:

software id is the sys id you want to query with, right? If not, change g_form.getValue('software_id') below to the correct field you want to use for the query.

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

    if (newValue === '') {
        g_form.clearValue('serverCollation');
    }


    var gaLocation = new GlideAjax('catalogHelper');
    gaLocation.addParam('sysparm_name', 'get_server_collation');
    gaLocation.addParam('sysparm_softwareID', g_form.getValue('software_id'));
    gaLocation.getXMLAnswer(handleResponse);

    function handleResponse(answer) {
        var profileObj = JSON.parse(answer);
        var s_collation = g_form.setValue('serverCollation ', profileObj.value, profileObj.displayValue);
        alert('s_collation' + s_collation);
    }
}

 

Script include:

Update/change the table and fields in the script below accordingly.

var catalogHelper = Class.create();
catalogHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    get_server_collation: function () {
        var grProfile = new GlideRecord('profile_table');

        if (grProfile.get(this.getParameter('sysparm_softwareID'))) {
            var profileObj = {};
            profileObj.value = grProfile.getValue('server_collation');
            profileObj.displayValue = grProfile.getDisplayValue('server_collation');
            return JSON.stringify(profileObj);
        }
    },

    type: 'catalogHelper'

});