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

Yes You can achieve using script include and onchange client script.

hi @Gopal14 

Yes you can use script include as bellow 

Script include:

var IncidentCountryHelper = Class.create();
IncidentCountryHelper.prototype = {
    initialize: function() {
    },

    // Function to get the country of a user and return the appropriate value
    getUserCountry: function(userSysId) {
        var userCountries = new GlideRecord('user_country_table');  // Replace with actual table name
        userCountries.addQuery('user', userSysId);  // Replace with the actual field if needed
        userCountries.query();

        var countryCount = 0;
        var countryToPopulate = '';

        // Loop through the countries and determine how many countries the user has
        while (userCountries.next()) {
            countryCount++;
            countryToPopulate = userCountries.country;  // Replace with the correct country field
        }

        // If only one country, return it; otherwise, return an empty string
        if (countryCount == 1) {
            return countryToPopulate;
        } else {
            return '';  // Multiple countries, return empty
        }
    },

    type: 'IncidentCountryHelper'
};

 

Now, you can call this Script Include from a Business Rule to populate the country field on the incident record.

 

Business rule:

(function executeRule(current, previous /*null when async*/) {
    // Create an instance of the Script Include
    var incidentHelper = new IncidentCountryHelper();
    
    // Get the assigned user's country (replace 'assigned_to' with the actual user field if different)
    var userCountry = incidentHelper.getUserCountry(current.assigned_to);  // Pass the assigned user Sys ID

    // If the user has one country, populate it on the incident; otherwise, clear the field
    current.country = userCountry;  // Will be empty if there are multiple countries or one country

})(current, previous);

 

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

thank you

Rajesh

 

Ashish Parab
Mega Sage

Hello @Gopal14 ,

 

You can try below script include and onChange client script.

 

Script Include - 

make sure client callable checkbox is true.

var GetCallerCountry = Class.create();
GetCallerCountry.prototype = {
    initialize: function() {},

    getCountryForCaller: function() {
        // Fetch the caller ID from the passed parameters
        var callerId = this.getParameter('sysparm_callerId');
        if (!callerId) {
            return ''; // Return empty if no caller ID is provided
        }

        // Query the user's country information
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(callerId)) {
            // Assuming `country` field exists on the user table
            var countryField = userGR.getValue('country');
            if (countryField) {
                var countries = countryField.split(','); // Split for multiple values
                if (countries.length === 1) {
                    return countries[0]; // Return the single country
                }
            }
        }
        return ''; // Return empty if no country or more than one country
    },

    type: 'GetCallerCountry'
};

 

OnChange Client script -

when caller <adjust as per your requirement> changes.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
        return;
    }

    var callerId = g_form.getValue('caller_id');
    if (!callerId) {
        g_form.setValue('country', '');
        return;
    }

    // Call the Script Include to fetch the single country or empty value
    var ga = new GlideAjax('GetCallerCountry');
    ga.addParam('sysparm_name', 'getCountryForCaller');
    ga.addParam('sysparm_callerId', callerId); // Pass caller_id
    ga.getXMLAnswer(function(response) {
        var country = response; // Response will be the country name or an empty string
        g_form.setValue('country', country);
    });
}

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish

Hi @Ashish Parab ,

 

Below is my script include:

var GetCallerLocation = Class.create();
GetCallerLocation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    initialize: function() {},

    getLocationForCaller: function() {
        // Fetch the caller ID from the passed parameters
        var callerId = this.getParameter('sysparm_callerId');
        if (!callerId) {
            return ''; // Return empty if no caller ID is provided
        }

        // Query the user's country information
        var userGR = new GlideRecord('sn_customerservice_csm_assignment_group_region');
        if (userGR.get(callerId)) {
            // Assuming `country` field exists on the user table
            var countryField = userGR.getValue('u_cmn_location');
            if (countryField) {
                var countries = countryField.split(','); // Split for multiple values
                if (countries.length === 1) {
                    return countries[0]; // Return the single country
                }
            }
        }
        return ''; // Return empty if no country or more than one country
    },
    type: 'GetCallerLocation'
});
 
 
Below is my client script:
 client script is on case table:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
var callerId = g_form.getValue('caller_id');
    if (!callerId) {
        g_form.setValue('u_cmn_location', '');
        return;
    }

    // Call the Script Include to fetch the single country or empty value
    var ga = new GlideAjax('GetCallerLocation');
    ga.addParam('sysparm_name', 'getLocationForCaller');
    ga.addParam('sysparm_callerId', callerId); // Pass caller_id
    ga.getXMLAnswer(function(response) {
        var country = response; // Response will be the country name or an empty string
        g_form.setValue('u_cmn_location', country);
    });
   //Type appropriate comment here, and begin script below
   
}
 
this is my custom table: sn_customerservice_csm_assignment_group_region
it is relationship b/w locations and groups.
 
In case form based on assigned to, region field(u_region) need to update, if assigned to is having one region it needs to autopopulate, if assgined to is having multiple regions it should be empty