Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Avoid updating same record twice when updating record on m2m table

Vidya Shree
Kilo Sage

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

5 REPLIES 5

Palak5
Tera Contributor

The issue is caused because the updateWithReferences() method is being called within a loop for each m2m record, potentially leading to multiple updates on the same sn_vul_vulnerable_item record if it is linked to multiple m2m records.

To avoid this, you need to ensure that each sn_vul_vulnerable_item record is updated only once. You can achieve this by maintaining a set of already processed sys_ids for the sn_vul_vulnerable_item records.