get value from parent table to child table

Akash68
Kilo Contributor

Hi,

in project table i have a field called "it segment".if that i"t segment" is equal to "xyz" then only i need to set some  values in child table(decision) like short description to "test"and approval required to yes

Please find the scripts used:

client script:table:Decission

function onLoad() {
//Type appropriate comment here, and begin script below
var itsegement=g_form.getValue("it_segments");------>it is in parent table
alert(itsegement);
var ga = new GlideAjax('SetValuesforITSDProject');-->my script include
ga.addParam('sysparm_name','myfunction');
ga.addParam('sysparm_user_name',g_form.getValue("it_segments"));
ga.getXML(setValue);
function setValue(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue("short_description",answer);
g_form.setValue("approval_required",'yes');
}}

 

script include:

var SetValuesforITSDProject = Class.create();
SetValuesforITSDProject.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
myfunction: function() {
var name = this.getParameter('sysparm_user_name').toString();
return "test";}

});

kindly help here. Thanks in advance!

1 ACCEPTED SOLUTION

Hi Akash,

If you are having field on child which refers to parent then Have display business rule and set the value in scratchpad variable for it segment.

use this scratchpad variable in onLoad and compare it with your sys id and then set the value for the required fields.

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

13 REPLIES 13

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Akash,

So what is your question here? What is not working?

On which table this client script is working? Also the description and approval_required fields are on what table i.e. parent or child?

Regards

Ankur

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

Dubz
Mega Sage

Hello,

If you have the it_segments field on the child form already why do you need the glide ajax? Have you extended that field from the parent to the child? If so then you should be able to just use that in your condition like:

if(g_form.getValue('it_segments') == 'this value'){

//add code 

}

if it isn't on the form then your itsegement variable will not have a value.

Cheers
Dave

Akash68
Kilo Contributor

Hi Ankur,

The client script is on "dmn_decision" table-->child table

description and approval required fields are also from "dmn_decision" table.

but "it segment" field is on parent table.--->project table

find_real_file.png

OK you can't use g_form.getValue to get the value of a field on the parent form. Use this code instead, you just need to pass the sys_id of the child record so you can retrieve the parent and then return the value of the it segment

client script:

function onLoad() {
if(isNewRecord()){   //i'm assuming you only want this to run when you first open the child ticket?
var ga = new GlideAjax('SetValuesforITSDProject');-->my script include
ga.addParam('sysparm_name','myfunction');
ga.addParam('sysparm_parent',g_form.getValue("parent"));
ga.getXML(setValue);
function setValue(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue("short_description",answer);
g_form.setValue("approval_required",'yes');
}
}
}

 

script include

var SetValuesforITSDProject = Class.create();
SetValuesforITSDProject.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
myfunction: function() {
var parent = this.getParameter('sysparm_parent');
var gr = new GlideRecord('pm_project');
if(gr.get(parent)){
return gr.it_segments.getDisplayValue();  //is this an OOB field? should be u_it_segments no?

}

});