Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Script help to copy fields and insert record into new table

Pat Surtan
Tera Expert

Hi Guys,

 

I want to copy some field values from one ticket into another ticket. The ticket 2 will be created once I click on a button from ticket 1 and the values from ticket 1 will copy into ticket 2. I am testing a very basic ui action script.

 

// (1) Copy request fields into a new incident

var gr = new GlideRecord("table2");

gr.initialize();

gr.short_description = current.short_description;

gr.insert();

 

The issue I am having is. Once I make a change in ticket 1 and save it, ticket 2 will automatically get created without me having to click the ui action in ticket 1. The short description field didn't even copy over.

Why is this happening?

2 REPLIES 2

Harshal Aditya
Mega Sage

Hi @Pat Surtan ,

 

Hope you are doing good.

Could you please check if there is any BR written which is inserting the record in the table2 ?

 

Please Mark My Response as Correct/Helpful based on Impact

Regards,
Harshal

 

Hello Harshal,

 

Yes, there is a business rule I created to copy reference field value from table 2 to reference field value to table 1. I believe this is the reason why it is inserting a ticket into table 2 every time I make a change. See the script below.

 

(function executeRule(current, previous /*null when async*/) {

var a=current.caller_id.getDisplayValue();
//gs.addInfoMessage(a); 

var gr=GlideRecord('table2');
current.caller_id = current.u_requested_for;
//gr.addQuery('number',current.change_request.number);
//gr.query();
//if(gr.next())
//{
// gs.addInfoMessage(gr.requested_by);
// gr.u_customer.setValue(a);
gr.update();


})(current, previous);

 

 

What's wrong with this script?