The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Update field on form from a ref field on same form

Dale Donahue
Tera Contributor

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

var RMBT_Project = Class.create();
RMBT_Project.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
initialize: function() {
 
var project_name = this.getParameter("sysparam_project_name");
var status_value = '';
var target = new GlideRecord('u_rmbt_project');
target.addQuery('u_initiative', project_name);
target.query();
 
if (target.next()) {
            status_value = target.u_status;
        }
 
return status_value;
},
 
    //type: 'RMBT_Project'
});
9 REPLIES 9

Karan Chhabra6
Mega Sage
Mega Sage

Hi @Dale Donahue ,

 

Please use the updated scripts below:

1. Client Script

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

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");
g_form.setValue('u_status_of_project', answer);
}
}

 

Script Include:

var RMBT_Project = Class.create();
RMBT_Project.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
initialize: function() {
 
var project_name = this.getParameter("sysparam_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'
});

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!

Hi Karan,

 

Thank you for the code update, but still appears that is not returning anything to update the field.

 

Not sure if it matters or not, but the "u_status_of_project" is a string field.

 

I did try to pull out the script include into scripts background and run it, this is what I got.

 

Script that was ran:

var project_name = 'AV Assessment';
var status_value = '';
var target = new GlideRecord('u_rmbt_project');
target.get(project_name);
status_value = target.u_status;
return status_value;

 

results:

Javascript compiler exception: invalid return (null.null.script; line 6) in:
var project_name = 'AV Assessment';
var status_value = '';
var target = new GlideRecord('u_rmbt_project');
target.get(project_name);
status_value = target.u_status;
return status_value;

I did get this to work(it does print the correct value), but if I try to use "Return" it says it is Null

 

var project_name = '2FA / Azure AD Password Self-Service Reset Imeplementation';
var status_value = '';
var target = new GlideRecord('u_rmbt_project');
target.get('u_initiative', project_name);
status_value = target.u_status;
gs.print(status_value)

Hi @Dale Donahue 

 

Check by returning value using the below code

var project_name = '2FA / Azure AD Password Self-Service Reset Imeplementation';
var status_value = '';
var target = new GlideRecord('u_rmbt_project');
target.get('u_initiative', project_name);
status_value = target.getValue('u_status');