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

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

you need to write a Script include and a client script to do this.

Take this a reference to start with https://community.servicenow.com/community?id=community_article&sys_id=c918c3b8db968c5013b5fb2439961918

 

Thanks,
Ashutosh Munot

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Better approach is to use GlideAjax instead of getReference()

you cannot set string value to a reference type of variable/field.

reference/field takes sys_id

Regards
Ankur

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

Is it not possible to auto populate reference variable from string field as their sys_ids are different.

Could you please give me an example how to right an glide AJAX for the mentioned requirement?

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