Update Affected location countries in a field

SM123
Tera Expert

Hi All,

URGENT HELP NEEDED : The requirement is to have field called Affected locations(LIst/String type) in incident form It should store the affected location's country data (country field is string type) when we add affected location in the related record using EDIT/new button. For that i wrote a BR on task_location table (after insert/update/delete  ). But it isn't working. 

 

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

    // Get the related Incident
    var incident = new GlideRecord('incident');
    if (!incident.get(current.task)) {
        return;
    }

    // Use a Set to avoid duplicate sys_ids
    var locSysIds = new Set();

    // Add already-selected locations in the list collector to avoid overwriting them
    if (incident.u_affected_locations) {
        var existing = incident.u_affected_locations.split(',');
        for (var i = 0; i < existing.length; i++) {
            locSysIds.add(existing[i].trim());
        }
    }

    // Query all task_location records for this task
    var taskLocGR = new GlideRecord('task_location');
    taskLocGR.addQuery('task', current.task);
    taskLocGR.query();

    while (taskLocGR.next()) {
        var loc = taskLocGR.location.getRefRecord();
        if (loc && loc.country) {

            // Now find all locations in the same country
            var locInCountryGR = new GlideRecord('cmn_location');
            locInCountryGR.addQuery('country', loc.country);
            locInCountryGR.query();

            while (locInCountryGR.next()) {
                locSysIds.add(locInCountryGR.getUniqueValue());
            }
        }
    }

    // Convert Set to comma-separated string
    incident.u_affected_locations = Array.from(locSysIds).join(',');
	
    incident.update();

})();

 

 

 

Thank You

2 REPLIES 2

Mark Manders
Mega Patron

You can't fill a list field with a string field from another table, because the list field contains the sysID of a record. It will always contain the display value of that record, not the 'country'. You could update the location table to contain the country records instead of strings and then do a look up to the country and fill the country sysID in the list field.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hi @Mark Manders ,

What if i use Parent field(reference type) from cmn_location table OR use affected location type as string instead of list. 

Because i can change my customization without disturbing existing behaviors.

Thank You