How to get Region, Geography and Site from Location table reference field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2024 05:14 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2024 06:46 AM
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:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2024 07:08 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 12:21 AM
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.