How to add a reference field's sys ID to a list field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2025 12:23 PM
I'm working with a client and need to be able to reference old companies that were previously assigned to an INC. It's for SLA reasons, but where I'm getting in stcuk is trying to add the previous companies on teh record, to a list field also on the table. The list field is a custom field called Previous Company list (u_previous_company_list).
I've created an After business rule when the company changes, but it doesn't seem to be working. It's been a while since I've needed to script something like this so I'm not sure where I messed up.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2025 08:24 PM
Hi @P_Haug
reading your hint that you have created an after Business Rule, I think in your code a final current.update() is missing. Otherwise, your changes are not saved back to the database. Or you change the BR type to before. In that case you don't need the final current.update() which is the preferred approach in ServiceNow.
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2025 08:28 PM
you can make your business rule as Before Update
Condition: Company Changes AND Company IS NOT EMPTY
Script:
(function executeRule(current, previous) {
// Get existing list or start new
var prevList = current.u_previous_company_list ? current.u_previous_company_list + ',' : '';
// Add previous company sys_id (if it exists)
if (previous.company) {
current.u_previous_company_list = prevList + previous.company.toString();
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2025 06:36 AM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2025 12:59 PM - edited ‎05-21-2025 12:59 PM
Company is a reference field, you will get one company at a time of update.
here is the code to add previous company in list, this code will add company to list without removing existing data on every update. Please change with your field names