UI Action to Copy Parent and Child Tables

rchadek
Kilo Contributor

We have a parent table (let's call it "intake") that currently has a UI Action "Copy Intake".

We also have some child tables that contain a reference field back to the parent. Each of these child tables ("Product 1", "Product 2", etc) also has a "Copy" UI Action ("Copy Product 1", "Copy Product 2", etc).

Right now, the child tables do not copy when using "Copy Intake". What we're trying to do is modify the "Copy Intake" UI Action so that the fields from the Intake table AND all the records from the associated child Product tables get copied over.

I think one challenge is, the children know about the parent, but the parent doesn't necessarily know about the children.

Could I query the child tables, create new Glide Records for each of them that contain records, then "while(product.next())" call the corresponding "Copy Product" UI Action from the "Copy Intake" UI Action?

1 ACCEPTED SOLUTION

ghsrikanth
Tera Guru

Yes, you are perfectly on right way of approach -


Could I query the child tables, create new Glide Records for each of them that contain records, then "while(product.next())"



in child tables, the parent record gets accessed by -



var grProd1 = new GlideRecord('Product1');


grProd1.addQuery('u_intake',current.sys_id);


grProd1.query();


while(grProd1.next()){


        //here copy all fields except one field


        //Intake field because here you have to put the new intake record that got created before


        var grNewProd1 = new GlideRecord('Product1');


        grNewProd1.initialize();


        grNewProd1.u_intake = <newIntake record sys_id>;


      //copy all fields from grProd1


      grNewProd1.insert();


}



Hopefully it helps


View solution in original post

10 REPLIES 10

ghsrikanth
Tera Guru

Yes, you are perfectly on right way of approach -


Could I query the child tables, create new Glide Records for each of them that contain records, then "while(product.next())"



in child tables, the parent record gets accessed by -



var grProd1 = new GlideRecord('Product1');


grProd1.addQuery('u_intake',current.sys_id);


grProd1.query();


while(grProd1.next()){


        //here copy all fields except one field


        //Intake field because here you have to put the new intake record that got created before


        var grNewProd1 = new GlideRecord('Product1');


        grNewProd1.initialize();


        grNewProd1.u_intake = <newIntake record sys_id>;


      //copy all fields from grProd1


      grNewProd1.insert();


}



Hopefully it helps


Thanks for the response. What's the best way to see if my query is returning any results? I put a gs.info statement after the query:



grProd1.query();


gs.info('Product 1 Row Count: ' + grProd1.getRowCount());



But where do I go to check the Row Count?


Hi Ryan,



The syntax should be gs.addInfoMessage();


And this should ideally come on the top of the form.


Scripting Alert, Info, and Error Messages - ServiceNow Wiki



Hopefully it helps


Hi Ryan,



gs.info should work. However I think the issue might be with the query itself.


Please make sure you are using the correct table names and column prefix. For ex custom table in global scope would be prefix with u_



Please share your script so that can help you further.