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
02-13-2018 02:49 PM
If anyone is able to find how the given sys_id is related to the attachment via the Service Portal, please let me know.
this id is populated when an attachment is added a record producer via a ServicePortal (widget), I would like to be able to capture it in the Record Producer Script
I have used my previous sys_id searcher tool found here
but no luck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2020 09:44 PM
i also tried the similar approach but the catalog ui policies and catalog client scripts configured to run on target record doesn't run or work. This is the issue for me, did you face similar issues and solve it? please let me know if you know a solution for my problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2018 11:02 PM
i have found a solution to making this work for both, Record producers via the Portal or via the Console.
script v2
Record Producer Script V2 and Steps to Implement
Enhancements
- Includes update to Record Producer Log table
- Support for attachments via both (portal and catalogue view)
- Fixes redirection issues inherited specifically from Helsinki instance.
Create record producer
- name: (TfNSW) Dynamic Record Producer Test DEMO
- table: sm_order
- Script variables to check/modify
- thisRPsysID – current sys_id of the RP
- childTable – table of ticket to create dependent on form selection
- Script:
//var message;
//Create Child Record base on selection of form
var childTable = 'hr_case';
var thisRPsysID = "184c35844f5e9700e8e846501310c751";
var gr = new GlideRecord(childTable); //set the table for the child record.
gr.initialize();
//gr.parent = current.sys_id; - testing only
//gr.(add whatever)
gr.short_description = producer.short_description;
gr.u_record_producer = thisRPsysID;
gr.description = 'Dynamic Record Producer Records';
var childTask = gr.insert();
//Move Attachments based on form in the PORTAL or CONSOLE
if(producer.attachmentGUID == "record_producer") //checks if default value is set
{
//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 = childTable ;
gr.table_sys_id = childTask ;
gr.update();
}
}
else
{
//for Record Producers on a Service Portal
// grabs attachment variable that was created on the form.
// this variable was populated and hidden via a widget client script
// source of the attachment ID is taken from the portal widget "reco" hiddent div tag has value bind to id
var gr =new GlideRecord('sys_attachment');
gr.addQuery('table_name','sp_portal');
gr.addQuery('table_sys_id',producer["attachmentGUID"] );
gr.query();
while(gr.next())
{
gr.table_name = childTable ;
gr.table_sys_id = childTask ;
gr.update();
}
}
//Variables
//Dynamically creates variables for child record
//Can't move variables as they aren't created yet.
// i.e producer.varableName1, producer.varableName2, … etc
var v;
var pastV;
var vID = "";
var order;
for (var key in producer)
{
v = producer[key];
if (pastV != v.getED()) //Exclude sequential duplicate variables
{
pastV = v.getED(); //sets past variable - skip sequential duplicates
vID = v.getED().toString().substring(2); //Gets sys_id of variable
if(v.getTableName() == 'variable') //Excludes other keys
{
// values that can be pulled from the producer[key]. Debugging.
//gs.addInfoMessage('P:/' + v + " - " + v.getDisplayValue() + " - " + v.getED() + " - " + vID );
//message = message + "<br/>" + key + "|" + v.getED() + "|" + v.getTableName() + "|" + v.getDisplayValue() + "|" + v + "|" + vID;
//Query to get order of the given variable
//Only important if using the 'variable editor' UI formatter
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 = childTable ;
gr3.table_sys_id = childTask ;
gr3.value = v;
gr3.question = vID;
gr3.order = order;
gr3.insert();
}
}
}
//gs.addInfoMessage(message);
//update sc_item_produced_record table which relates a Task (table: task) record to the Record Producer (table: sc_cat_item_producer) that created the Task.
var rpTask = new GlideRecord("sc_item_produced_record");
rpTask.initialize();
rpTask.record_key = childTask;
rpTask.record_table = childTable;
rpTask.task = childTask;
rpTask.producer = thisRPsysID;
rpTask.insert();
//redirect to child record6
if(producer.attachmentGUID == "record_producer")7
{
producer.redirect= "/" + childTable + ".do?sys_id=" + childTask; //non portal redirect
}
else
{
producer.portal_redirect = "?id=ticket_nsw&table=" + childTable + "&sys_id=" + childTask; //portal redirect
}
//stops parent record being created,
//stops parent variables being created
current.setAbortAction(true);
Amend Existing Record Producer widget (sc-catalog-item)
- Add Attachment Guid HTML code
- This line binds the generated ID into this element for retrieval via the widget script, it is added as a hidden DIV at the top of the form
<div style="visibility:hidden;display:none;" id="_attachmentGUID" ng-bind="c.data._attachmentGUID"></div>
Create Widget Client Controller
- Name: (TfNSW) Dynamic Record Producer – Attach
- Description: Collects the attachment ID from the reco widget to then store in the hidden varaible for processing
- Client Script:
-
function($scope) { var c = this; var attachID = document.getElementById('_attachmentGUID'); var g_form = $scope.page.g_form; var g_field = $scope.page.field; g_form.setValue("attachmentGUID", attachID.innerHTML); g_form.setReadOnly("attachmentGUID", true); // Hide variable after testing - must be dont this way to preserv value var attachmentGUID_field = document.getElementById('c59fb9084f5e9700e8e846501310c7bd'); // variable field attachmentGUID_field.style.display = "none"; attachmentGUID_field.style.visibility = "hidden"; //g_form.setDisplay("test_variable",false); // stops submission of field }
Create Variable – Place holder for Macro Widget Script
- Question: attachmentGUID
- Name: attachmentGUID
- Default Value: record_producer
- Type: single Line Text
Create Variable – MACRO runs script to populate other variable with attachment ID
- Type: Macro
- Question: get_attachmentGUID
- Name: get_attachmentGUID
- Widget: (TfNSW) Dynamic Record Producer - Attach
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2018 11:33 PM
updates to Record Producer script
better filtering for reference variables
additional script to run workflow on child task
if( (v.getTableName() == 'variable' || !v.getTableName())) //Excludes other keys // null is used cause reference variables not showing
{
var vOrder = new GlideRecord("item_option_new");
if (vOrder.get("sys_id", vID))
{ /****/
gr3.insert();
}
}
//Set ADHOC workflow to run
var gr2 = new GlideRecord(childTable);
gr2.get(childTask);
var wf = new Workflow();
var wfId = wf.getWorkflowFromName("****AdHoc");
var vars = {};
vars.u_approver = producer.approver_1;
//vars.u_app ='';
//vars.u_a_phase ='';
wf.startFlow(wfId, gr2, "insert", vars);