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

Weird
Mega Sage

Your current record is in the u_hardware_expensed table? You can reference it in the business rule by using current.
Current is the object of the record you're working on. 

GlideRecord is basically the same thing, but you retrieve some other record.

var inc = new GlideRecord('incident');
inc.addQuery('number', 'XXXX');
inc.query();

if(inc.next()){
var x = inc.number;
}

For example, in the above script we retrieve any incident record with number field XXXX. This will get us a list of records that fit the criteria. inc.next() tells the script to jump on the next available record. I'm using if here' so even if there's 100 records, we are only doing our code for the first one found. while allows you to loop through all of them and do separate things for them. Just note that addQuery is used between new GlideRecord() and inc.query() since it's the filter you're using to query the database. You can't use it anymore inside if or while loop to change the original query.


So, in your BR you can do this:

var gr = new GlideRecord('u_hardware_expensed_asset_list');
gr.initialize();
gr.<field_name> = current.<field_name>;
gr.insert();

 Here I'm telling the script to initialize a new empty record on the "u_hardware_expensed_asset_list" table.
I'm then telling it to set the field <field_name> on that table to the current records <field_name> fields value.
Then I insert the record into the specified table.

So, if you have a field called related_hardware, which is a reference to the current record, you could add

gr.related_hardware = current.getUniqueValue();

OR

gr.related_hardware = current.sys_id;


Some fields are choices or reference fields and can cause confusion.
For example if you tried to say field x = current.state on incident record, you could get "2" instead of "Work in Progress". That's because the script gives you the value instead of the display value. Usually that's fine, but sometimes you want to just display the label/name of the target/choice, so you can say:

 inc.state.getDisplayValue();

 
The last thing for you to note are the Business Rule's settings.
There're options to trigger the BR on insert or update. Insert means when you're creating a new record and update when updating an existing one. There's also a when to run option. Before means before saving the changes and after is done after changes are done.

The main difference between the when to run options is that with before rule you can say current.<field_name> = "something" and it will save that value. On after rule if you add that the value will not be saved, unless you use current.update(), which you should never use as that can cause issues. Note that you can use gr.update() when updating another record.


In your case if you're not looking to update anything on the current record, you can just use After insert/update rule. Probably just update if you're waiting for approved to change. If it can be approved when the record is created, then select insert as well.
Then use and modify the second script I wrote to create a new record in the asset list table.

 

Let me know if this is a bit confusing since I didn't put too much thought on the structure.