Dynamic Record Producer Records

restevao
Giga Expert

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

 find_real_file.png

 

 

 

find_real_file.png

 

 

 

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);
10 REPLIES 10

I am not able to set values for reference field. How to do on that ?