- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2016 08:00 AM
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?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2016 08:13 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2016 11:03 PM
Thanks again for your response. It really helped. I'm almost there.
I can see that I'm creating 2 separate Parent Sys ID's and that my query of the Child Table is returning rows.
I am making copies of the Children, but they're getting attached to the original form, not the new one that was copied.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2016 11:08 PM
Could you please check the bolded line, are you attaching the newly created intake sys_id?
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2016 08:48 AM
Yes, I've double-checked that I'm assigning the new Intake record sys_id to the reference field in the Child table. Maybe it's the order in which I'm performing insert/updates?
Here's the basic script I'm using:
// Gather all current Intake fields and copy to new Intake Glide Record
intake.fields = current.fields;
var sysID = intake.insert();
current.intake_id = sysID; // This is the new Intake record's sys_id
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 = current.intake_id; // Assign new Intake Record's sys_id to the Child's reference field
//copy all fields from grProd1
grNewProd1.insert();
}
var mySysID = intake.update();
I'm logging out info messages to the syslog table and getting the following values:
gs.info('Product Row Count: ' + grProd1.getRowCount()); // Returns 5 (correct)
gs.info('Current Sys ID: ' + current.sys_id); // Return's the original Intake record's sys_id
gs.info('Current Intake ID: ' + current.intake_id); // Returns the new Intake record's sys_id
gs.info('My Sys ID: ' + mySysID); // Returns the new Intake record's sys_id
Please let me know what you think.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2016 09:05 AM
You can directly assign the value -
grNewProd1.u_intake = sysID; //instead of current.intake_id
Could you please try the above.
Ideally this should work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2016 10:20 AM
I just tried that and I'm still getting the same result. The Child tables are copying over to the original Parent rather than the newly created one.
I logged this to double-check:
gs.info('New Product Sys ID: ' + grNewProd1.u_intake);
The variable grNewProd1.u_intake is being assigned the new Intake's Sys ID.
So, the field 'u_intake' on the Child table is a reference field to the Parent Table. The Display Label of this reference field is "IO Number" and the number shown is something like ORD1234567.
The Parent table then has a variable 'number' that is a String value from the Task table. The Display Label is "Number" and the display value follows the same pattern ORD1234567.
Since the Child field references the Parent Table which then references the Task table, could that be the problem? Maybe I need to dot-walk or something?