Populate or Clear 'Country' Field in Incident Table Based on User's Assigned Countries

Gopal14
Tera Contributor

Hi Team,

 

I have a custom field called country in incident table, If user is having one country we need populate that country in country field in incident table. If user is having more than one country, then country field should be empty in incident table.

 

for this I have created relationship for groups and locations, that relationship is added in the groups table as a related list

 

Note: Table and field name are not same, will change.

8 REPLIES 8

NagaChandaE
Tera Guru

Hi @Gopal14 

 

where you defining the countries for specific user in user table.

Ct111
Giga Sage

Below is  a sample script that you can take as a reference

like  a

Business rule on incident before insert or update

 

 

(function executeRule(current, previous /*null when async*/) {
// Get the current user (Caller)
var userSysId = current.caller_id;

if (!userSysId) {
// If Caller ID is not set, clear the country field
current.u_country = '';
return;
}

// Query the relationship table (sys_user_grmember, sys_user_has_location, or custom table)
var countryList = [];
var relationshipGR = new GlideRecord('your_relationship_table'); // Replace with your table
relationshipGR.addQuery('user', userSysId); // Replace 'user' with the correct field for user relationship
relationshipGR.query();
while (relationshipGR.next()) {
countryList.push(relationshipGR.getValue('country')); // Replace 'country' with your actual field name
}

if (countryList.length === 1) {
// If only one country, set it in the field
current.u_country = countryList[0];
} else {
// If multiple countries or none, leave it blank
current.u_country = '';
}
})(current, previous);

 

 

 

I hope this information helps.

Rajesh Chopade1
Mega Sage

hi @Gopal14 

You can create a Business Rule (After Insert / Update) that triggers when an incident is created or updated. This rule will check the user’s countries and populate the country field on the incident accordingly.

(function executeRule(current, previous /*null when async*/) {
    // Get the user's countries (replace 'user_country' and 'locations' with the actual table names/field names)
    var userCountries = new GlideRecord('user_country_table');  // Adjust table name as needed
    userCountries.addQuery('user', current.assigned_to);  // Replace with actual user field
    userCountries.query();

    var countryCount = 0;
    var countryToPopulate = '';

    // Loop through the countries and determine how many countries are assigned
    while (userCountries.next()) {
        countryCount++;
        countryToPopulate = userCountries.country;  // Adjust to the correct country field
    }

    // If only one country, populate the country field on the incident
    if (countryCount == 1) {
        current.country = countryToPopulate;  // Populate the country field on the incident
    } else {
        // If multiple countries, clear the country field
        current.country = '';  // Clear country field if there are multiple countries
    }

})(current, previous);

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

 

 

Can we achieve this using script include