Populate related list with records from reference field

nabilo_
Giga Contributor

Hi,


I need some help in a complex requirement:


1. I have a Table called PO

2. I have another Table called POL

This first table PO is the master one, and the second table POL is a related table with a field referencing to the PO parent table.

This means that you can have one record in PO with multiple POLs. and I do have a Related List displaying all POLs for every PO record.

Now the Complexity is the fact that I need to add a new Reference Field to the PO table, "called PM" that is a reference to the ProductModule "sc_cat_item" table.

When a User selects one PM from the Product Modules "sc_cat_item", I need to take all what this PM Includes in it's related list "Includes" and put these records in the POL related list of my PO.

My Incomplete Script:  

Condition: PM field Changes.

function onBefore(current, previous) {

    //This function will be automatically called when this rule is processed.

    var gr = new GlideRecord("proc_po_item");  

gr.addQuery("purchase_order",current);  

gr.query();

while(gr.next()){

gr.purchase_order = current.sys_id;

gr.requested_for = current.requested_by;

gr.part_number = ...;

}  

}

I know the script is incomplete, and might even be wrong, but I need to know how to:

1. Create records in the POL related List of my PO table.

2. Fill the fields of this POL record with data from the PM record

3. Loop till I have all the PM related list "Includes" added as POLs.


Best regards,

Nabil O.

1 ACCEPTED SOLUTION

Hi Nabil,



Make sure you query the reference table related list and loop through the records and get the object. Once done query the related table(which is the related list for parent table) and create the records with its object.


Also please make sure that column names are correct when you construct an query.



I hope this helps


View solution in original post

9 REPLIES 9

joshua_bice
Giga Expert

Nabil,



I'm having a bit of trouble understanding exactly what you're asking for here. Do you just need to update the POLs when the parent PO is updated, or is there more you're hoping to do?



As for the related list, you can accomplish that through a UI Action. You can pretty much just copy and paste other "New" UI actions for related lists and change a couple of fields.


Hi Joshua,



I would like to thank you first for your reply, and explain to you my requirement, it's not simply creating new records in the related list, it's in fact creating records based on records from another table "sc_cat_item". for every record in the SCI table that exists in the "Includes" related list, i want to copy it and create it as a record in my POL table related to PO.



Table 1. PO


Table 2. POL > Related List in PO Table


Table 3. SCI


Table 3 again. SCI > Related List in SCI "Includes"


Field: PO.PM >reference to SCI


POP.png



Best regards,



Nabil O.


nabilo_
Giga Contributor

and Guess What, I felt it was too simple to be true, so I came to the understanding today that the Relationship 4 "in Orange" is a Many2Many relationship. So there will be another table to be considered here one of the m2m Tables.


Is the related list on SCI(PM) table is from POL ?