how to auto create child record when multiple mrvs are added

alishamulan
Tera Contributor

alishamulan_0-1746516231578.png

hello, 

i want to write a businees rule i have created   mrvs in record producer
when i add rows then its child record must also be created in mrvs , how can i achieve this?

how to link child to parent in business rule

1 ACCEPTED SOLUTION

mohdarbaz
Kilo Guru

Hi @alishamulan ,

 

you can use below sample script to link child to parent in BR.

 

(function executeRule(current, previous /*null when async*/) {
    // Query the child table
    var grChild = new GlideRecord('child_table');
    grChild.addQuery('parent_reference_field', current.sys_id);
    grChild.query();
   
    // Loop through each child record and update the parent reference field
    while (grChild.next()) {
        grChild.parent_reference_field = current.sys_id;
        grChild.update();
    }
})(current, previous);
 

This script ensures that each child record is linked to its parent record whenever the parent record is inserted or updated.

 

If my response helped, please mark it correct/helpful and close the thread so that it benefits future readers.

 

Regards,

Mohd Arbaz.

View solution in original post

5 REPLIES 5

mohdarbaz
Kilo Guru

Hi @alishamulan ,

 

you can use below sample script to link child to parent in BR.

 

(function executeRule(current, previous /*null when async*/) {
    // Query the child table
    var grChild = new GlideRecord('child_table');
    grChild.addQuery('parent_reference_field', current.sys_id);
    grChild.query();
   
    // Loop through each child record and update the parent reference field
    while (grChild.next()) {
        grChild.parent_reference_field = current.sys_id;
        grChild.update();
    }
})(current, previous);
 

This script ensures that each child record is linked to its parent record whenever the parent record is inserted or updated.

 

If my response helped, please mark it correct/helpful and close the thread so that it benefits future readers.

 

Regards,

Mohd Arbaz.

Gaurav Rathaur
Kilo Guru

HI @alishamulan ,

 

To achieve this in a Business Rule when using MRVs in a Record Producer, follow these steps:

  1. Create the Business Rule:

    • Table: Set the Business Rule to trigger on the Record Producer's table (e.g., sc_cat_item).

    • When: Choose "After Insert" to trigger after the record is created.

  2. Link Child Record to Parent:

    • Inside the Business Rule, use GlideRecord to create the child records in the related MRV table.

    • Link the child record to the parent using the parent record's Sys ID.

(function executeRule(current, previous /*null when async*/) {
    // Assuming 'mrvs' is the field holding MRV values in the Record Producer
    var mrvs = current.mrvs;  // Access MRV variable from the record producer

    // Iterate through the MRV entries
    for (var i = 0; i < mrvs.length; i++) {
        var childRecord = new GlideRecord('your_child_table');
        childRecord.initialize();
        childRecord.parent_field = current.sys_id;  // Link child to parent
        childRecord.child_field = mrvs[i].value;  // Set your MRV value
        childRecord.insert();  // Insert child record
    }
})(current, previous);

 

Steps:

  • Replace your_child_table with the actual table for child records.

  • Replace parent_field with the field that links the child record to the parent (sys_id of the parent).

  • Replace child_field with the field to store the MRV value.

This will create the child records in the MRV table, linking them to the parent record.

 

If my response was helpful, please mark it as the correct answer and close the thread. This will help others who come across the same issue.

Thanks!
Gaurav Rathaur

Ankur Bawiskar
Tera Patron
Tera Patron

@alishamulan 

you can use record producer script if you are showing MRVS on record producer or after insert business rule

check this link and enhance

How to create records in a table from MRVS? 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@alishamulan 

Seems you created thread for this already and response was already given to that

If my response helped please mark it correct and close the thread so that it benefits future readers.

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