- 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-01-2022 07:04 AM
Hi Sai Pavan,
You can achieve using after insert update business rule.
Thanks,
Murthy
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 07:35 AM
Can you please help me with the script that needs to be used in business rule ?
- 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-01-2022 10:45 PM
Hope you are doing good.
Did my reply answer your question?
If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader