The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to insert a record from one table to another upon creation if a certain field is true?

Kennyyhh
Tera Contributor

Hi, new to coding and ServiceNow.

 

I've had to come up with a scenario. I've got a table called Hardware Expense Requests. Fields include Product Name, Brand, Hardware Approved, Hardware Type, Location, Quantity and Source. I want the records in this table to copy to another table called Hardware Expensed Asset List if the hardware approved field for that record is true. I'm trying to create a business rule and my javascript is very basic.  So having a bit of trouble knowing what script to use from doing research online. I don't really understand what to fill in and what operators I am supposed to use. This is what I have so far. Not sure what I am missing. Help is much appreciated.

Screen Shot 2022-10-24 at 5.35.40 pm.png

 

2 ACCEPTED SOLUTIONS

H_9
Giga Guru

Hello,

 

Please following the following steps:

 

1. Go to Business rules list

2. Create New

3. Give any name

4. In table field select "Hardware Expense Requests" and check the "advanced" check box as true

5. In "When" field select "Asynch" or "After" (any one of these is fine)

6. Check the check box "Insert" as true

7. Condition : Hardware Approved is True

8. Go to Advanced section

9. Write the following script in it

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

	var gr = new GlideRecord("u_hardware_expensed_asset_list");
	gr.initialize();
	gr.<field_name1> = current.<field_name1>;
	gr.<field_name2> = current.<field_name2>;
	gr.<field_name3> = current.<field_name3>;
	//repeat above for all the required fields
	gr.update();

})(current, previous);

10. Save

 

Now test by inserting a record in the first table, it will insert the same record in another table.

Please mark the answer correct if it helps. 🙂

Thanks. 🙂

View solution in original post

The names of the fields which you have used with current object and gr are incorrect.

Please use the backend column names of these fields.
In the table columns, please bring "column name " field as well from where you will be able to get the correct name of the field as shown below example. You have to take the name of the field from "Column name" column.

You always have to use the correct backend names, otherwise the system will not understand which field to copy and the record will be empty

H_9_0-1666598430673.png

Please mark the answer correct if it helps. 🙂

 

 

Thanks. 🙂

View solution in original post

5 REPLIES 5

Edgar10
Tera Guru

Hi Kenny,

 

It would be better if you used Flow Designer.

 

Regards,

Edgar

H_9
Giga Guru

Hello,

 

Please following the following steps:

 

1. Go to Business rules list

2. Create New

3. Give any name

4. In table field select "Hardware Expense Requests" and check the "advanced" check box as true

5. In "When" field select "Asynch" or "After" (any one of these is fine)

6. Check the check box "Insert" as true

7. Condition : Hardware Approved is True

8. Go to Advanced section

9. Write the following script in it

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

	var gr = new GlideRecord("u_hardware_expensed_asset_list");
	gr.initialize();
	gr.<field_name1> = current.<field_name1>;
	gr.<field_name2> = current.<field_name2>;
	gr.<field_name3> = current.<field_name3>;
	//repeat above for all the required fields
	gr.update();

})(current, previous);

10. Save

 

Now test by inserting a record in the first table, it will insert the same record in another table.

Please mark the answer correct if it helps. 🙂

Thanks. 🙂

Kennyyhh
Tera Contributor

I think I may have done it incorrectly. The record keeps coming up as empty in the second table. These are the fields in the asset list table which I've made sure was exactly the same as the fields in the request table.

Screen Shot 2022-10-24 at 6.37.22 pm.png

Screen Shot 2022-10-24 at 6.34.51 pm.png

The names of the fields which you have used with current object and gr are incorrect.

Please use the backend column names of these fields.
In the table columns, please bring "column name " field as well from where you will be able to get the correct name of the field as shown below example. You have to take the name of the field from "Column name" column.

You always have to use the correct backend names, otherwise the system will not understand which field to copy and the record will be empty

H_9_0-1666598430673.png

Please mark the answer correct if it helps. 🙂

 

 

Thanks. 🙂