Dynamic Record Producer Records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2018 01:58 AM
This script allows for a Record Producer based on a Parent Table to create records on a child table.
Basically, from any table create records on any other table.
This is a custom configuration, managed by a single script in a record producer.
Tasks
Doesn't create parent record (optional, can be switch)
Reduce DB size
Variables
Transfers variables (dynamically)
Preserves Variable Order
Doesn't create variable records for parent record (optional, can be switch)
Reduce DB size
Attachments
Transfers attachment to Child Record (varies for both Record Producers in Portals and Not in portals)
Variables created from a Record Producer are stored on the question_answer table
This Script is able to dynamically obtain the variables used on the current record producer. It will identify them
via its sys_id and then query where necessary to obtain additional information
Record Producer Script and Import XML
//Create Child Record
var gr = new GlideRecord('incident');
gr.initialize();
//gr.parent = current.sys_id; - testing only
//gr.(add whatever)
gr.short_description = producer.short_description;
gr.description = 'learn about GlideRecord';
var incident = gr.insert();
//Copy any Attachments
//GlideSysAttachment.copy('task',current.sys_id,'incident',incident);
//for Record Producers NOT on a Service Portal
// Move Attachment - Find the Original and rename the attachement ID
//Attachments are stored in DB as soon as they are uploaded.
var gr =new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id',current.sys_id);
gr.query();
while(gr.next())
{
gr.table_name = 'incident' ;
gr.table_sys_id = incident ;
gr.update();
}
/*
//for Record Producers on a Service Portal — untested but logic will work 99% of the time
// look for last attachment uploaded by user — use its ID to query for other attachments to be moved
var gr =new GlideRecord('sys_attachment');
gr.addQuery('table_name','sp_portal');
gr.addQuery('sys_created_by',gs.getUserName());
gr.orderByDesc('sys_created_on');
gr.setlimit(1);
gr.addQuery('sys_mod_count',0);
gr.addQuery('sys_created_on',gs.daysAgo(0));
gr.query();
if(gr.next())
{
var gr2 =new GlideRecord('sys_attachment');
gr2.addQuery('table_sys_id', gr.table_sys_id);
gr2.query();
while(gr2.next())
{
gr2.table_name = 'incident' ;
gr2.table_sys_id = incident ;
gr2.update();
}
}
*/
//Can't move variables as they aren't created yet.
//Manually create variables — stored in 'question_answer' table
// i.e producer.varableName1, producer.varableName2, … etc
//Stop extra bulk variables been created in the DB
//var gr2 =new GlideRecord('question_answer');
//Dynamically creates variables for child record
var v;
var pastV;
var vID = "";
var order;
for (var key in producer)
{
v = producer[key];
if (pastV != v.getED()) //Exclude duplicate variables / sequential
{
pastV = v.getED(); //set past variable - skip sequential duplicates
vID = v.getED().toString().substring(2); //Get sys_id of variable
if(v.getTableName() == 'variable') //Exclude other keys
{
//gs.addInfoMessage('P:/' + v + " - " + v.getDisplayValue() + " - " + v.getED() + " - " + vID ); // values that can be pulled from the producer[key]. Debugging.
//Query to get order of the given variable
//Only important if using the 'variable editor' UI formatter, this exists on the task table out-of-box, it will have to be modified to show on the incident(any other) table.
var vOrder = new GlideRecord("item_option_new");
if (vOrder.get("sys_id", vID)) order = vOrder.order;
//Insert variable into Question Answer Table.
var gr3 = new GlideRecord('question_answer');
gr3.initialize();
gr3.table_name = 'incident' ;
gr3.table_sys_id = incident ;
gr3.value = v;
gr3.question = vID;
gr3.order = order;
gr3.insert();
}
}
}
//redirect to child record
producer.redirect= "/incident.do?sys_id=" + incident ;
//producer.redirect= current ;
//stops parent record being created,
//stops parent variables being created
current.setAbortAction(true);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2019 05:53 PM
There's a simpler way to accomplish this. On the Record Producer script, do your mappings from record producer variable to child and parent fields from the table as below, however at the end just add this single line of code: current.sys_class_name = <child_table_name>;
Example:
// current refers to the parent table field
current.short_description = producer.short_description;
current.description = 'learn about GlideRecord';
current.sys_class_name = 'incident'; // child table name
This will allow you to create a record automatically in the child table, from the parent table record producer without having to handle the form being aborted or initialising a new GlideRecord. With this method, the sys_id's of the child and parent records will automatically match.
Kind regards,
Omar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2019 09:37 PM
This is really cool. Works for me
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 05:39 AM
i remember that field.
that is a brilliant idea, where were you a year ago.
ill try it an see how i go.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2019 01:35 PM
I'm trying this method but the record producer is setting the number on the record as TASKxxxxx. What I want is to create an incident record or else a sn_customerservice_case (CSM) record based on a choice on the form. Do you now if that is possible using this method? Or would I need to use the widget clone method above?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2019 04:16 PM
Yes you can still use this method if you're submitting the case via a record producer. You just need to add an if condition that checks the value of the choice field in the record producer script, and do the field mapping/setting of the child table based on it.