How to populate/display Child records with reference to Parent table

AbdulAzeez
Mega Guru

Hello Everyone,

I have 2 list fields 1-Product & 2- Application based on Product(Parent) selection i need to display child records in Application list record. Using Service Offering table as Reference for both the list records

Example

If i select   (IT Service Management)   in Product list then Application list should display all the relevant records like Incident Management, Problem Management, Change Management etc

Please help me out to achieve this

1 ACCEPTED SOLUTION

Hi Abdul,


Try the script below.


A few points to note:


1. I did not use advance reference qual in Annu or in Ayesha, so if you have any, you can remove them.


2. adding more than one parent to Annu field will not update Ayesha with the new records.


3. removing a parent from the Annu field will not remove child records from Ayesha; it can probably be done - I do not know how.


However, adding two parent values to Annu will show the first value's added children in Ayesha. Then, removing the first value from Anu (the one that displays the children) will show in Ayesha the second value's children.


4. both of my fields - Annu and Ayesha - refer to the service offering table. Therefore, the records they show are the display records (name) from the Service Offering table. I do not know of a way to show the parent field display value in Annu, and the child record name in Ayesha when they are both list fields on the same table. If a list field is not a must, we can create a drop down that references the parent from   Service Offering.



Here is the onChange script for Annu:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue === '') {


  return;


  }



  //Type appropriate comment here, and begin script below


  var annu = g_form.getValue('u_annu');


  var tfm = new GlideRecord('service_offering');


  tfm.addQuery('sys_id', annu);


  tfm.query();


  while(tfm.next()) {


  var list = tfm.parent;


  var array = list.split(',');


  for(var i=0; i<array.length;i++) {


  var p = new GlideRecord('service_offering');


  p.addQuery('parent', array[i]);


  p.query();


  p.query(getMeTheNames);


  }


  }


}




function getMeTheNames(p) {


  var list = new Array();


  while (p.next()) {


  list.push(p.sys_id);


  }


  g_form.setValue('u_ayesha', list);


}



harel


Please mark as correct or helpful as needed.


View solution in original post

16 REPLIES 16

Hi Abdul,


Try the script below.


A few points to note:


1. I did not use advance reference qual in Annu or in Ayesha, so if you have any, you can remove them.


2. adding more than one parent to Annu field will not update Ayesha with the new records.


3. removing a parent from the Annu field will not remove child records from Ayesha; it can probably be done - I do not know how.


However, adding two parent values to Annu will show the first value's added children in Ayesha. Then, removing the first value from Anu (the one that displays the children) will show in Ayesha the second value's children.


4. both of my fields - Annu and Ayesha - refer to the service offering table. Therefore, the records they show are the display records (name) from the Service Offering table. I do not know of a way to show the parent field display value in Annu, and the child record name in Ayesha when they are both list fields on the same table. If a list field is not a must, we can create a drop down that references the parent from   Service Offering.



Here is the onChange script for Annu:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue === '') {


  return;


  }



  //Type appropriate comment here, and begin script below


  var annu = g_form.getValue('u_annu');


  var tfm = new GlideRecord('service_offering');


  tfm.addQuery('sys_id', annu);


  tfm.query();


  while(tfm.next()) {


  var list = tfm.parent;


  var array = list.split(',');


  for(var i=0; i<array.length;i++) {


  var p = new GlideRecord('service_offering');


  p.addQuery('parent', array[i]);


  p.query();


  p.query(getMeTheNames);


  }


  }


}




function getMeTheNames(p) {


  var list = new Array();


  while (p.next()) {


  list.push(p.sys_id);


  }


  g_form.setValue('u_ayesha', list);


}



harel


Please mark as correct or helpful as needed.


Harel,



Thats working like a charm!



Just a doubt if i change Annu List type to Reference field type then what should i change ?




Thanks alot Harel.. Happy Weekend!


Good Morning Harel,



I am hope you are doing great.



Your above script is working fine but i want to populate multiple fields selected in parent table. Could you please help me out.



https://community.servicenow.com/message/1126412#1126412


I am not sure an onChange is what you need, because the list field captures the first addition to the field, but when you add another record without removing the first one, it does not. See point 2. Maybe a save to the form before populating the second field?


Harel,



I tried by saving and again adding one more record but still it is displaying older values only not adding new parent fields.