Avoid updating same record twice when updating record on m2m table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2024 06:42 AM
Hi All,
I am writing a fix script to update records on the m2m table. Since its m2m relationship one record has multiple records linked to it(just like sys_user_has_role table).So, Whenever i am updating something it is updating the same record multiple times. How can i avoid this?
Thanks
Vidyashree
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2024 06:46 AM
it might be going into recursion
share your script along with screenshots.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2024 06:58 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2024 09:52 AM - edited 12-17-2024 09:55 AM
It's probably due to updateWithRefereces(), change to 'm2m.update(); and test:
updateWithReferences(Object reason)
Updates a record and also inserts or updates any related records with the information provided.
GlideRecordAPI_GlideRecord-updateWithReferences
Seem you will need to add logic to update the dot-walked fields.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2024 03:52 PM
Seems a dangerous API (updateWithReverences()) if table relationships are not known. using the example in the documentation, for a non-existing record:
var inc = new GlideRecord('incident');
inc.get(inc_sys_id); // Looking up an existing incident record where 'inc_sys_id' represents the sys_id of a incident record
inc.caller_id.first_name = 'John';
inc.caller_id.last_name = 'Doe';
inc.updateWithReferences();
when run in Scripts - Background results in:
Showing affected tables. The View full summary link shows: