Populate branch code,pincode,address for similar city and state

kirangorde09069
Tera Contributor
  • IMG20231230195722.jpg

     Basically I have two table one is the city table in that table my all data are stored red mentioned on the above picture and second table is bank account table in that table I have similar fields so when I select a state and city according to that my branch code, pincode, address get populated in bank account table how to write a code for that in servicenow

2 REPLIES 2

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @kirangorde09069 

 

It is not a direct or starlight forward. For this you need to write a code and that take city and run a loop to get the details.

 

Have a look in below link . These are reference link to give you basic idea

 

https://www.servicenow.com/community/developer-forum/how-would-you-populate-city-and-state-fields-on...

 

 

https://www.servicenow.com/community/developer-forum/how-can-i-auto-populate-street-city-state-zip-o...

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

KasiramanathanD
Tera Contributor

Hi @kirangorde09069 ,

 

1. Dependency Setup in Bank Account Table:
- Configure the `City` field in the Bank Account table to be dependent on the `State` field. This ensures that the available values in the `City` field are dynamically updated based on the selected value in the `State` field.

2. Onchange Client Script for City Field:
- Implement an onchange client script on the `City` field in the Bank Account table with the following functionality:
- If the `City` field changes to an empty value, clear the associated fields such as `Branch Code`, `Pin Code`, and `Address`.
- If the `City` field is not empty, invoke a Script Include using GlideAjax, passing the `State` and `City` values as parameters.
- The Script Include should query the `Cities` table, and if a record is found, retrieve the `Branch Code`, `Pin Code`, and `Address` and return them in a JSON object.
- Populate the `Branch Code`, `Pin Code`, and `Address` fields in the Bank Account table based on the received values from the Script Include.


Onchange Client Script:

function onChangeCity(control, oldValue, newValue, isLoading, isTemplate) {
if (!isLoading && newValue === '') {
// City value changed to empty, clear associated fields
g_form.setValue('branch_code', '');
g_form.setValue('pin_code', '');
g_form.setValue('address', '');
} else if (!isLoading && newValue !== '') {
// City is not empty, call the Script Include to fetch details
var stateValue = g_form.getValue('state'); // Assuming you have a state field

// Call the Script Include using GlideAjax
var ga = new GlideAjax('CityDetailsScriptInclude');
ga.addParam('sysparm_name', 'getCityDetails');
ga.addParam('sysparm_state', stateValue);
ga.addParam('sysparm_city', newValue);
ga.getXML(onSuccess);

function onSuccess(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
var details = JSON.parse(answer);

// Set values in the form
g_form.setValue('branch_code', details.branchCode);
g_form.setValue('pin_code', details.pinCode);
g_form.setValue('address', details.address);
}
}
}


Script Include:

var CityDetailsScriptInclude = Class.create();
CityDetailsScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCityDetails: function() {
var state = this.getParameter('sysparm_state');
var city = this.getParameter('sysparm_city');

var details = {};

// Query the Cities table to get details based on state and city
var cityGr = new GlideRecord('cities_table'); // Replace 'cities_table' with your actual table name
cityGr.addQuery('state', state);
cityGr.addQuery('city', city);
cityGr.query();

if (cityGr.next()) {
// Populate details if the record is found
details.branchCode = cityGr.branch_code.toString();
details.pinCode = cityGr.pin_code.toString();
details.address = cityGr.address.toString();
}

// Return details as JSON
return new JSON().encode(details);
}
});


Note: Remember to adapt the field names and table names based on your actual data model.