Can you add a variable editor from a parent to child record?

e_wilber
Tera Guru

This question has been asked a few times over the past few years but I don't see a definitive answer.

Scenario:
1) You have a Case record producer that generates a Case.
2) The agent determines an Incident needs to be generated and creates a child Incident
3) We want the child incident record to have the original variable editor as the case does so all the information from the original form is visible

4 REPLIES 4

Kieran Anson
Kilo Patron

You could potentially clone the com_glideapp_questionset_default_question_editor ui macro and create a new one where instead of using "current.getUniqueValue()" you pass in the sys_id of the parent record for the incident.

I had a similar requirement recently, and what I did instead was

  1. Fire a custom event "map_producer_vars_to_child_record"
  2. Use a script action to copy the question_answer, sc_multi_row_question_answer, and sc_item_produced_record entries...effectively duplicating them to the child record

This would have a momentary delay, but allowed for the incident to appear as if the record producer created it.

 

Example script, will need to adjust for your needs

(function mapVars(current /**Case Record**/, childTable , childSysId){
	var tableName = current.getTableName();
	
	var childRecordGR = new GlideRecord(childTable);
	if(!childRecordGR.isValid())
		throw new Error(gs.getMessage("Invalid Table provided for variable mapping.\n Case: {0} \n Child Table {1} , Child ID {2}" , [current.getDisplayValue() , childTable , childSysId]));
	
	if(!childRecordGR.get(childSysId))
		throw new Error(gs.getMessage("Invalid Record ID provided for variable mapping.\n Case: {0} \n Child Table {1} , Child ID {2}" , [current.getDisplayValue() , childTable , childSysId]));
	
	
	var qaMap = {};

	var qaGR = new GlideRecord("question_answer");
	qaGR.addQuery("table_name" , tableName);
	qaGR.addQuery("table_sys_id", current.getUniqueValue());
	qaGR.query();
	while(qaGR.next()){
		var existingVal = qaGR.getUniqueValue();
		qaGR.setValue("table_name" , childRecordGR.getTableName());
		qaGR.setValue("table_sys_id" , childRecordGR.getUniqueValue());
		var newVal = qaGR.insert();
		qaMap[existingVal] = newVal;
	}
	
	var mrqGR = new GlideRecord("sc_multi_row_question_answer");
	mrqGR.addQuery('parent_table_name' , tableName);
	mrqGR.addQuery('parent_id' , current.getUniqueValue());
	mrqGR.query();
	while(mrqGR.next()){
		mrqGR.setValue("parent_table_name" , childRecordGR.getTableName());
		mrqGR.setValue("parent_id" , childRecordGR.getUniqueValue());
		
		var qaNew = qaMap.hasOwnProperty(mrqGR.getValue('question_answer')) ? qaMap[mrqGR.getValue('question_answer')] : mrqGR.getValue('question_answer');
		mrqGR.setValue('question_answer' , qaNew);
		
		mrqGR.insert();
	}
	
	var itemProducedGR = new GlideRecord("sc_item_produced_record");
	itemProducedGR.addQuery("task" , current.getUniqueValue());
	itemProducedGR.setLimit(1);
	itemProducedGR.query();
	if(itemProducedGR.next()){
		itemProducedGR.setValue("task" , childRecordGR.getUniqueValue());
		itemProducedGR.setValue("record_table" , childRecordGR.getTableName());
		itemProducedGR.setValue("record_key" , childRecordGR.getUniqueValue());
		itemProducedGR.insert();
	}
	
})(current, event.parm1 , event.parm2);

I have question here. Variables are not read only... Are those variables editable from child record? I tried but it's not updating when edit and save. 

Thanks for posting , it's really helpful we can modify OOTB UI Macro(line 9) to add our child record as -catItemProducedGr.addQuery("record_key", current.getUniqueValue()).addOrCondition("record_key", current.parent.getUniqueValue());

No Impact.

Thanks!

Michael Hays
Tera Expert

My Company just had to do this. I created scoped application extended from the task table. When a record producer is used to create a ticket and then may need to be split off into child tasks.

Mine works with multi row variables (MRVS) too. We have multiple MRVS that need to be passed into the child task.

 

Steps

1. Create a ui macro - based off the default question editor

MichaelHays_1-1709652159020.png

 

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <g:requires name="scripts/js_includes_catalog.js" includes="true"/>
    <g:requires name="styles/${new CatalogCssSelector().getVariableCss()}" includes="true"/>
    <g:if_polaris>
       <g:then>
         <g:requires name="styles/${new CatalogCssSelector().getPolarisVariableCss()}" includes="true"/>
      </g:then>
    </g:if_polaris>
    <g2:evaluate var="jvar_catalog_item">
       function eval_cat_item() {
        var cat_item = "";
        var catItemProducedGr = new GlideRecord("sc_item_produced_record");
        catItemProducedGr.addQuery('task', current.parent.sys_id);// <---- parent sys_id from task table to get variables -----
        catItemProducedGr.query();
        if (catItemProducedGr.next())
            cat_item = catItemProducedGr.getValue("producer");
        return cat_item;
       }
       eval_cat_item();
    </g2:evaluate>
   
    <!-- load only the one_to_one variable sets -->
    <g2:evaluate var="jvar_cat_sets" jelly="true">
       var jvar_cat_sets = "";
       var gr = new GlideRecord("io_set_item");
       gr.addQuery("sc_cat_item", jelly.jvar_catalog_item);
       gr.addQuery("variable_set.type", "one_to_one");
       gr.query();
       while (gr.next()) {
          if (jvar_cat_sets.length > 0)
             jvar_cat_sets += ",";
          jvar_cat_sets += gr.variable_set;
       }
       //gs.addInfoMessage('jvar ' + jvar_cat_sets);
       jvar_cat_sets;
    </g2:evaluate>
   
    <g2:evaluate var="jvar_questionset_read_only" jelly="true">
        (gs.getProperty('glide.sc.evaluate.variables.write.access', true) == 'true') ? !current.variables.canWrite() : !current.canWrite();
    </g2:evaluate>
 
    <div data-sn-macro-sys-id="${jvar_macro_sys_id}">
        <j2:if test="$[jvar_catalog_item != '']">
            <g2:evaluate jelly="true">  
            var recordObject = new GlideRecord('name of your table');<------ change to your table name -------
            recordObject.get(current.parent.sys_id);        
            </g2:evaluate>
            <j2:set var ="ref" value="recordObject" />
            <j2:set var="jvar_producer_target_record" value="true"/>
            <g2:client_script type="catalog_question_editor" catalogItem="$[jvar_catalog_item]"/>
            <g:inline template="catalog_ui_policy.xml"/>
            <g2:render_component componentName="com.glideapp.servicecatalog.DefaultQuestionEditor"/>
        </j2:if>
        <j2:if test="$[jvar_catalog_item == '']">
            <!--Render old default editor if catalog item is not found -->
            <g2:render_component componentName="com.glideapp.questionset.DefaultQuestionEditor"/>
        </j2:if>
    </div>
</j:jelly>
 
 
2. Create a ui formatter that points to the table you want and uses the above macro name in the formatter field
MichaelHays_4-1709652433460.png

 

 


 

3. add the variable editor to your form from configure form option

MichaelHays_3-1709652384193.png