How to add a reference field's sys ID to a list field

P_Haug
Tera Contributor

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.

 

 

 

(function executeRule(current, previous /*null when async*/) {

    // Add your code here
if (current.company) {
    var compSysId = current.company;
    var arr = [];

    var prevComps = new GlideRecord('core_company');
    prevComps.addQuery('sys_id', 'IN', compSysIds);
    prevComps.query();

    while (prevComps.next()) {
        arr.push(prevComps.toString());
    }

    current.u_previous_company_list = arr.toString();
}


})(current, previous);
5 REPLIES 5

Maik Skoddow
Tera Patron
Tera Patron

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

Ankur Bawiskar
Tera Patron
Tera Patron

@P_Haug 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@P_Haug 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Muhammad Salar
Giga Sage

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

if (current.company.changes()) {
        var previousCompanySysId = previous.company.toString();
        var existingHistory = current.u_previous_companies.toString();

        if (existingHistory) {
            var companyArray = existingHistory.split(',');
            if (!companyArray.includes(previousCompanySysId)) {
                companyArray.push(previousCompanySysId);
                current.u_previous_companies = companyArray.join(',');
            }
        } else {
            current.u_previous_companies = previousCompanySysId;
        }
    }