Update field on form from a ref field on same form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2023 06:21 PM
Hi all,
New to JavaScript, what I'm looking to do is I have the caller setting a ref field and from setting that ref field I want to set the value of another field on the same form from the same table that the ref field is using.
Example:
ref field on form: u_reference_1
other field to set on form from ref value: u_status_of_project
u_reference_1 is looking at table u_rmbt_project pulling column u_initiative
What I'm trying to set is the "status" field from whatever the u_initiative status is.
I have tried to setup a client side script with an script include, but I'm doing something wrong.
Client side
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var project_name = g_form.getValue('u_reference_1.value');
var ga=new GlideAjax('RMBT_Project');
ga.addParam('sysparm_project_name', project_name);
ga.getXML(RMBT_Project);
function RMBT_Project(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_status_of_project', answer);
}
}
Include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2023 07:00 AM
@Dale Donahue return statements can be written only within a method/function, if you are trying to return in the BG Script without a method/function it will not work.
the below will fail in a BG script as the return is not in a method.
var a=10;
return a;
Return works perfectly within a method:
var a=10;
gs.print(sum(a));
function sum(a){
return a;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2023 09:00 AM
How can I confirm that return is getting a value, because when I try to run this fully to see if the fields update, the field is just blank. I have the field as a string right now, so I did type in some random things and after changing the project_name field the status field goes blank. Seems like the return is still returning NULL to the client side.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2023 07:10 AM - edited 06-28-2023 07:13 AM
@Dale Donahue Just realized that you are referring to an incorrect parameter name in the script include, changed the line to the parameter "sysparm_project_name". Originally script was referring to "sysparam_project_name" but the parameter sent by the client script was different so I had changed the line in the script include
var RMBT_Project = Class.create(); RMBT_Project.prototype = Object.extendsObject(AbstractAjaxProcessor, { initialize: function() { var project_name = this.getParameter("sysparm_project_name"); var status_value = ''; var target = new GlideRecord('u_rmbt_project'); target.get(project_name); status_value = target.u_status; return status_value; }, //type: 'RMBT_Project' });
Please mark the appropriate response as correct answer and helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2023 09:00 AM
Thank you, I did update that. Still not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2023 09:53 PM
@Dale Donahue I would recommend you to add alert statements to see if the script is getting executed.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
alert('In Client Script');
var project_name = g_form.getValue('u_reference_1');
var ga=new GlideAjax('RMBT_Project');
ga.addParam('sysparm_project_name', project_name);
ga.getXML(RMBT_Project);
function RMBT_Project(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert('Inside get XML mehod, response is'+answer);
g_form.setValue('u_status_of_project', answer);
}
}
Script Include
var RMBT_Project = Class.create();
RMBT_Project.prototype = Object.extendsObject(AbstractAjaxProcessor, {
initialize: function() {
gs.addErrorMessage('Executing Script Include');
var project_name = this.getParameter("sysparam_project_name");
gs.addErrorMessage('Project Name-'+project_name);
var status_value = '';
var target = new GlideRecord('u_rmbt_project');
target.get(project_name);
status_value = target.u_status;
gs.addErrorMessage('Status Retrieved-'+status_value);
return status_value;
},
//type: 'RMBT_Project'
});
Please mark the appropriate response as correct answer and helpful