- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 06:55 AM
Hi Community,
I have a requirement where - 'If a record gets inserted / Updated in a custom table then same respective record should be created / updated in another custom table'. The Two tables have same set of fields. What script needs to be used in this type of scenario ?
Solved! Go to Solution.
- Labels:
-
Customer Service Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 08:13 AM
Considering you have Table A as source and Table B(custom table) and Table B has reference field pointing to Table A (this reference field will help you knowing which record of Table B to update)
you need After Insert & Update BR on Table A
Script like this: Enhance it further as per your case
(function executeRule(current, previous /*null when async*/) {
// Add your code here
if(current.operation() == 'insert'){
var gr = new GlideRecord("tableB");
gr.initialize();
gr.u_fieldA = current.u_fieldA;
gr.u_fieldB = current.u_fieldB;
gr.u_referenceField = current.sys_id;
gr.insert();
}
else if(current.operation() == 'update'){
var rec = new GlideRecord('tableB');
rec.addQuery("u_referenceField", current.sys_id);
rec.query();
if(rec.next()){
rec.u_fieldA = current.u_fieldA;
rec.u_fieldB = current.u_fieldB;
rec.update();
}
}
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2022 12:36 AM
Hi,
you can achieve it through flow and there you can set trigger condition on current.
use create record, update record action and select table as custom table and select fields from trigger condition which is your first custom table fields.
For this created and update you can create 2 flows with trigger as create and updated or you can select only created or updated and using look up records action you can set create or update.
Ex: select look up records action table as your custom table1(assumption number is unique field) and condition select as number is matches with trigger condition table number then it will update the data.
else it will create the record.
If you use the flows there will be no code or less code required.
using business rule:
Create a After business rule on your first custom table. check both insert and update fields.
Script:
(function executeRule(current, previous /*null when async*/ ) {
var grtab2 = new GlideRecord('table2');
grtab2.addQuery('number', current.number); //you can query the records based unique fields.ex: incident number
grtab2.query();
if (grtab2.next()) {
//Update records
grtab2.field1 = current.field1; //here updating the values on fields
grtab2.field2 = current.field2;
grtab2.field3 = current.field3;
grtab2.field4 = current.field4;
grtab2.update();
} else {
//create new records
grtab2.initialize();
grtab2.field1 = current.field1; //here updating the values on fields
grtab2.field2 = current.field2;
grtab2.field3 = current.field3;
grtab2.field4 = current.field4;
grtab2.insert();
}
}
)(current, previous);
Hope you it helps you.
Please Mark ✅ Correct/Helpful if applicable, Thanks!!
Regards
Pavankumar
ServiceNow Community MVP 2024.
Thanks,
Pavankumar