Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get Region, Geography and Site from Location table reference field

sameershind
Tera Contributor

Hello Everyone,

 

I have Location field on Project record and i have created Geography, Region and Site field on Project table. I want to get these three values when I select location on Project record. 

 

Please assist.

 

Regards,

Sameer

3 REPLIES 3

Yashsvi
Kilo Sage

Hi @sameershind,

To achieve this in ServiceNow, you can use a Business Rule to populate the Geography, Region, and Site fields based on the Location field on the Project record.

Creating the Business Rule :-

- When: Select 'before'

- Insert: Check this box if you want the rule to run on insert.

- Update: Check this box if you want the rule to run on update.

- Filter conditions: Add a condition if you only want to run the rule when the Location field is not empty.

 

Script:

if (current.location) {
    var locationGR = new GlideRecord('cmn_location');
    if (locationGR.get(current.location)) {
        current.u_geography = locationGR.u_geography;
        current.u_region = locationGR.u_region;
        current.u_site = locationGR.u_site;
    }
}
(current, previous);

By following these steps, you should be able to automatically populate the Geography, Region, and Site fields on the Project record based on the selected Location using a Business Rule in ServiceNow.

 

Please mark helpful, if you accept the solution.

SAI VENKATESH
Kilo Patron
Kilo Patron

Hi @sameershind 

 

You can try the below code and modify the code based upon your requirement

 

onchange client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var loc = g_form.getValue('location');
    alert(loc);

    var ga = new GlideAjax('GetManagerDetailsScript');
    ga.addParam('sysparm_name', 'getlocation1');
    ga.addParam('sysparm_location', loc);
    ga.getXMLAnswer(callback);

    function callback(response) {
        var answer = response;
        answer=answer.split(',');
		g_form.setValue('latitude',answer[0]);
		g_form.setValue('longitude',answer[1]);

    }
    //Type appropriate comment here, and begin script below

}

Script Include:

var GetManagerDetailsScript = Class.create();
GetManagerDetailsScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getlocation1: function(){
		gs.info("into getlocation")
        var requestedFor = this.getParameter('sysparm_location');
        var grcountry1 = new GlideRecord('cmn_location');
        grcountry1.addQuery('sys_id', requestedFor);
        grcountry1.query();
        if (grcountry1.next()) {
			gs.info("the latitude is "+grcountry1.latitude);
            return  grcountry1.latitude +','+grcountry1.longitude;
        }

	},
   



});

 

Thanks and Regards

Sai Venkatesh

SasankaV
Giga Guru

 

Hi @sameershind, I think I faced a similar use case in my experience with Work4flow. Give the below steps a try:

  • Log in to your ServiceNow instance and navigate to the Project table.

  • Create Geography, Region, and Site fields on the Project table if not already created.

  • Navigate to the Client Scripts module.

  • Create a new onChange client script for the Location field on the Project form.

  • Add the following script to populate Geography, Region, and Site fields based on the selected Location:

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

  // Check if the state is Qualify
  if (current.state == -4) {
    
    // Trigger the survey (modify the survey trigger logic as per your setup)
    var surveyUtil = new SNC.SurveyUtils();
    surveyUtil.triggerSurvey(current);

    // Recalculate metric results (modify as per your existing metric calculation logic)
    var metricUtils = new MetricUtils();
    metricUtils.calculateMetricResults(current);

  }

})(current, previous);

Let me know if it doesn't work. I'll be happy to assist.