Set value on table from template_value field

NeilH2
Giga Guru

I have a field which is typed as a template_value on a record.

I want to take the values from the field and apply them to another record.

I've done this before and i just cannot think of the code that does it although i'm fairly certain its one - two lines at most.

Can anyone help?

1 ACCEPTED SOLUTION

Nick65
Mega Expert

Hey Neil, I actually had a similar requirement and found you can do something like this,



var gr = new GlideRecord('table_with_template_value_field');


gr.get('someSysID');


var values = ''+gr.u_set_values;         //this is your template field



  var inc = new GlideRecord('anytable');


  inc.get('someSysID');


 


          if (values){


                inc.applyEncodedQuery(values);  


                inc.update()                                         //could also be used in inc.insert()


                }



View solution in original post

7 REPLIES 7

Hi Mike



The field i'm template_value field i'm updating from is in a custom table, and that script requires the template "name" from the sys_template table





Regards


Neil Holden


Pradeep Sharma
ServiceNow Employee

Hello Neil,



You need to create an AFTER business rule and update the target table field value as



var field = current.PASS REFERENCE FIELD NAME HERE.getRefRecord();


field.PASS TARGET FIELD NAME HERE = 'test';


field.update();


NeilH2
Giga Guru

So a friend of mine came up with the below code to split the string and apply each value individually.



var gr = new GlideRecord("custom_template_table");


gr.addQuery("sys_id","2ff41bd1db8d8300fe71f3571d961997");


gr.query();


gr.next();



var test = gr.u_set_field_values;


var firstSplit = test.split("^"); //split by the caret icon


gs.log('Query String ' + firstSplit );



//loop through each split value and apply them to the record - ignore the EQ


for(var i = 0; i < firstSplit.length; i++){



    if(firstSplit[i] != "EQ"){


        var secondSplit = firstSplit[i].split("=");


        gs.print(secondSplit[0] + "   -   " + secondSplit[1]);


        inc.setValue(secondSplit[0],secondSplit[1]);


    } else {


          continue;


    }


}


inc.update();


}