- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2020 05:15 AM
I need to write logic to keep a glide_list field sync with the related list on that form. In other words, there is a table Y in the related lists of table X, all the records of table Y should get displayed on table X list field. (Table X and Y have m2m mapping).
Could anyone please help..
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2020 07:36 PM
Hi Kanika,
Try below scripts and do testing
sample script below for the insertion
var rec = new GlideRecord('table X');
rec.addQuery('sys_id', current.<fieldX>);
rec.query();
if(rec.next()){
var listArray = rec.<listField>.toString().split(',');
var valueOfY = current.<fieldY>;
listArray.push(valueOfY.toString());
rec.<listField> = listArray.toString();
rec.update();
}
Sample script for deletion
var rec = new GlideRecord('table X');
rec.addQuery('sys_id', current.<fieldX>);
rec.query();
if(rec.next()){
var listArray = rec.<listField>.toString().split(',');
var valueOfY = current.<fieldY>;
var indexOf = listArray.indexOf(valueOfY);
listArray.splice(indexOf, 1);
rec.<listField> = listArray.toString();
rec.update();
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
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
03-16-2020 06:25 AM
Hi Kanika,
you would require to have after insert/update business rule on the m2m table;
BR Condition: value Y changes; query and update the record of table X; if there is addition then get the current value and append it
On similar line you would require after delete Business rule on m2m table to remove the value being deleted
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
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
03-16-2020 04:12 PM
BR Condition: value Y changes; query and update the record of table X; if there is addition then get the current value and append it
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2020 07:36 PM
Hi Kanika,
Try below scripts and do testing
sample script below for the insertion
var rec = new GlideRecord('table X');
rec.addQuery('sys_id', current.<fieldX>);
rec.query();
if(rec.next()){
var listArray = rec.<listField>.toString().split(',');
var valueOfY = current.<fieldY>;
listArray.push(valueOfY.toString());
rec.<listField> = listArray.toString();
rec.update();
}
Sample script for deletion
var rec = new GlideRecord('table X');
rec.addQuery('sys_id', current.<fieldX>);
rec.query();
if(rec.next()){
var listArray = rec.<listField>.toString().split(',');
var valueOfY = current.<fieldY>;
var indexOf = listArray.indexOf(valueOfY);
listArray.splice(indexOf, 1);
rec.<listField> = listArray.toString();
rec.update();
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
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
03-17-2020 10:10 AM
Hi Kanika,
Any update on this?
Can you mark my answer as ✅ correct, 👍 helpful if you were able to achieve the requirement. This enables other members to learn from this thread.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader