The CreatorCon Call for Content is officially open! Get started here.

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

Yes, of-course it would have to be changed. Anyway, I take it back...


Here is an onChange code to change Ayesha and show all relevant records based on several parents in Annu.


A plus: removing parents from Annu removes children from Ayesha; also clearing Annu clears Ayesha.


Yeah! (an achievement to my meager coding skills )



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


  if (isLoading) {


  return;


  }



  //Type appropriate comment here, and begin script below


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


  if(newValue=='') {


  g_form.clearValue('u_ayesha');


  }



  var list = annu;


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


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


  var tfm = new GlideRecord('service_offering');


  tfm.addQuery('sys_id', array[j]);


  tfm.query();


  while(tfm.next()) {


  var list1 = tfm.parent;


  var array1 = list1.split(',');


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


  var p = new GlideRecord('service_offering');


  p.addQuery('parent', array1[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 helpful or correct based on impact


Its working Harel.. Thank you so much..



I too learnt a lot with this.. I am very slow in scripting hope this teaches me a lot.