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

So you actually want to populate a list-type field based on a selection from another list-type field?


If you don't need multiple choice in the Annu field, I suggest to turn it into a reference or choice field.


In the below example, I am using a category field, which is a choice list field, to choose a value from.


Then, in the list field, called "drop" (u_drop), I expect values to show up. The values are from another table (called my_table), which the category field appears on.


My "drop" field (your Ayesha) has the advanced reference qualifier similar to what I gave you earlier.


Try this:


Type: onChange --> of the category field (similar to your Annu)


Script:


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


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


          return;


  }



  //Type appropriate comment here, and begin script below


  var category = g_form.getValue('category').toString();


  var list = category;


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



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



  var stnd = new GlideRecord('u_my_table');


  stnd.addQuery('u_category', array[i]); //category field on my_table


  stnd.query();


  stnd.query(getMeTheNames); //using a callback function


  }


}


function getMeTheNames(stnd) {


  var list = new Array();


  while (stnd.next()) {


  list.push(stnd.sys_id);


  }


  g_form.setValue('u_drop', list);


}



Let me know if that helps.


harel


Please mark as correct or helpful based on impact


Harel,



Just have couple of doubts



1. Is your reference qualifier syntax is right ? : javascript:'u_annu='+current.u _annu;



2. My both fields are the from the same table & as per client request Annu type should be List only. Still i can try with your code ?


Harel,



i pasted a same code and changed category and field names but still it is not working.





Capture.PNG



reference qualifier:



Capture2.PNG



Please let me know if i am doing anything wrong...


I am probably a bit confused and not clear on your setup then.


So you have Service Offering table that has records in it. Each record has a service type - Product or Application and a parent. The parent will show in Annu and the its child records will show in Ayesha. Correct?


Harel,



You are correct. Service Offering table has a Service type choice field which consist of - Product or Application.



Whenever i select any Parent table example: ITSM is a Parent record which has a Child records as Incident, Change & Problem, Parent record i.e ITSM should falls in Annu & child recrods i.e Incident, Problem, Change must fall in Ayesha table.