Auto populate Reference field with value based on 6 options selected in choice field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2021 02:13 AM
Hello All,
I have a requirement where i need to auto populate reference field based on option selected in choice field.
Note: reference field has n values but i need to populate only 6 values based on options selected in choice field.
lets assume checkbox field as CB, Reference field as Ref if we select CB value as Abc then Reference value should populate as Abc(entry present in reference field).
I have written one script on change but as i need to populate sys_id's its working only at the time of on change when i tried to submit its showing invalid update.
Could anyone please help me with script include to populate only these selected 6 values based on choice.
Thanks,
Raj
- Labels:
-
Release Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2021 03:10 AM
Hi Raj,
Here's an easy that I have done this which may work in your scenario. Setup your choice list with the Label Abc and the Value '1232..' - that is the sys_id of the record on the referenced table which corresponds to Abc. Then your onChange script is just populating the reference field the same as the choice field. If setting up the choice list like this won't work for your situation (the choice field now has a sys_id string value instead of storing 'Abc') then you'll need your onChange script to call a Script Include which will query the reference table and return the sys_id of the record that matches the choice Value, then populate the reference field with that sys_id. Both of these solutions populate the actual value of the reference field with a valid sys_id that will remain when the update is submitted. If what you really want to do is filter your reference field based on a choice field value, then you're talking about using an advanced reference qualifier that calls a script include passing in the choice value, then returns a list of sys_ids of the only records which should be available to be selected, but reference qualifiers don't actually set the value of the field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2021 03:51 AM
Hello Brad,
Thank you so much for your prompt response.
Could you please help me with Sample script for below case which works for my scenario if you have handy.
you'll need your onChange script to call a Script Include which will query the reference table and return the sys_id of the record that matches the choice Value, then populate the reference field with that sys_id
Thanks,
Raj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2021 04:32 AM
Your onChange Client Script when the choice field changes will look something like this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('refUtils');//name of Script Include
ga.addParam('sysparm_name','getRefValue');//name of function within Script Include
ga.addParam('sysparm_choice',newValue);
ga.getXML(hello);
function hello(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('reference_field_name', answer);//your field name
}
}
Next create a Script Include using the same name as is used in the GlideAjax call, ensuring the Client callable box is checked. Your script will look a little something like this, depending on the reference table and field names. I've added a bunch of temporary logging so you can see where the breakdown is if/when something doesn't work right the first time.
var refUtils = Class.create();
refUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRefValue: function() {
var answer = '';
var choice = this.getParameter('sysparm_choice');
gs.info('refSI choice = ' + choice);
//retrieve the record on the reference field table that is related to the choice value
var refGR = new GlideRecord('reference_table_name');//your table name
refGR.addQuery('related_field_name', choice);//your field name
refGR.query();
if(refGR.next()){
gs.info('refSI record found ' + refGR.sys_id);
answer = refGR.sys_id.toString();
}
gs.info('refSI answer ' + answer);
return answer;
},
type: 'refUtils'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2021 03:21 AM
Hi Raj,
Please refer below link do changes accordingly it might help you:
I hope this will help you.
If it works then kindly mark the answer as correct & close the thread.
Kind Regards,
Tejas Tamboli