drop down in a widget

radt
Kilo Guru

Hello, I have a drop down which is getting called by widget..

I have countries drop down and all the countries are in order whereas International is on top but I want it on the bottom of the list.. code is: 

(function() {

data.category = $sp.getParameter('sys_id');
var otherCountries = gs.getProperty('sn_hr_sp.Other Countries').toString();
var userID = gs.getUserID();
var selectedCountryID = $sp.getParameter('country_id')||'';
if(selectedCountryID=='')
{
var userGR = new GlideRecord('sys_user');
userGR.addQuery('sys_id',userID);
userGR.query();
if(userGR.next())
{
selectedCountryID = userGR.getValue('u_core_country');
}
}
//include All and Global options
var countryArr = [
// {id: 'all', title: gs.getMessage("All")},
// {id: 'other', title: gs.getMessage("Other")}
//{id: 'other', title: gs.getMessage("Other countries")}
{id: 'international', title: gs.getMessage("International")}
];

data.selectedIndex =0;


// Get Accessible Countries Associated to Portal.
var portalGR = $sp.getPortalRecord();
var countryIDs = portalGR.getValue('u_countries');

data.countries = countryIDs.split(',');

data.sc_catalog_page = $sp.getDisplayValue("sc_catalog_page") || "esc_sc_category";

var gr = new GlideRecord("core_country");
gr.addQuery('sys_id', 'IN', countryIDs);
gr.orderBy('name');
gr.query();
while(gr.next()){
var countrySysID = gr.getDisplayValue("sys_id");
var countryName = gr.getDisplayValue("name");
var countryObj = {id: countrySysID, title: countryName};
countryArr.push(countryObj);
}
//Set selected country to Other if users country is one of the 'Other' countries
if(otherCountries.indexOf(selectedCountryID) >= 0){
data.selectedIndex = 0;
data.countryName = countryArr[0].name;
}

//set selected country in country picker
var index = 0;
for(var i in countryArr){
//Find position of selected country in array
if (selectedCountryID == countryArr[i].id){
data.selectedIndex = index;
data.countryName = countryArr[i].name;
//gs.addInfoMessage(data.countryName);
}
index++;
}
data.countryArr = countryArr;
data.countryID=selectedCountryID;

})();

 

 

How can I make International get to bottom. We recently changed other countries to International.

 

TIA 🙂 

 

@Ankur Bawiskar 

1 ACCEPTED SOLUTION

Abhijit4
Mega Sage

Try below code :

 

(function() {

data.category = $sp.getParameter('sys_id');
var otherCountries = gs.getProperty('sn_hr_sp.Other Countries').toString();
var userID = gs.getUserID();
var selectedCountryID = $sp.getParameter('country_id')||'';
if(selectedCountryID=='')
{
var userGR = new GlideRecord('sys_user');
userGR.addQuery('sys_id',userID);
userGR.query();
if(userGR.next())
{
selectedCountryID = userGR.getValue('u_core_country');
}
}
//include All and Global options
var countryArr = [];

data.selectedIndex =0;


// Get Accessible Countries Associated to Portal.
var portalGR = $sp.getPortalRecord();
var countryIDs = portalGR.getValue('u_countries');

data.countries = countryIDs.split(',');

data.sc_catalog_page = $sp.getDisplayValue("sc_catalog_page") || "esc_sc_category";

var gr = new GlideRecord("core_country");
gr.addQuery('sys_id', 'IN', countryIDs);
gr.orderBy('name');
gr.query();
while(gr.next()){
var countrySysID = gr.getDisplayValue("sys_id");
var countryName = gr.getDisplayValue("name");
var countryObj = {id: countrySysID, title: countryName};
countryArr.push(countryObj);
}
var internationalCountry={id: 'international', title: gs.getMessage("International")};
countryArr.push(internationalCountry);

//Set selected country to Other if users country is one of the 'Other' countries
if(otherCountries.indexOf(selectedCountryID) >= 0){
data.selectedIndex = 0;
data.countryName = countryArr[0].name;
}

//set selected country in country picker
var index = 0;
for(var i in countryArr){
//Find position of selected country in array
if (selectedCountryID == countryArr[i].id){
data.selectedIndex = index;
data.countryName = countryArr[i].name;
//gs.addInfoMessage(data.countryName);
}
index++;
}
data.countryArr = countryArr;
data.countryID=selectedCountryID;

})();

Please mark answer as Correct or Helpful based on impact.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

2 REPLIES 2

Abhijit4
Mega Sage

Try below code :

 

(function() {

data.category = $sp.getParameter('sys_id');
var otherCountries = gs.getProperty('sn_hr_sp.Other Countries').toString();
var userID = gs.getUserID();
var selectedCountryID = $sp.getParameter('country_id')||'';
if(selectedCountryID=='')
{
var userGR = new GlideRecord('sys_user');
userGR.addQuery('sys_id',userID);
userGR.query();
if(userGR.next())
{
selectedCountryID = userGR.getValue('u_core_country');
}
}
//include All and Global options
var countryArr = [];

data.selectedIndex =0;


// Get Accessible Countries Associated to Portal.
var portalGR = $sp.getPortalRecord();
var countryIDs = portalGR.getValue('u_countries');

data.countries = countryIDs.split(',');

data.sc_catalog_page = $sp.getDisplayValue("sc_catalog_page") || "esc_sc_category";

var gr = new GlideRecord("core_country");
gr.addQuery('sys_id', 'IN', countryIDs);
gr.orderBy('name');
gr.query();
while(gr.next()){
var countrySysID = gr.getDisplayValue("sys_id");
var countryName = gr.getDisplayValue("name");
var countryObj = {id: countrySysID, title: countryName};
countryArr.push(countryObj);
}
var internationalCountry={id: 'international', title: gs.getMessage("International")};
countryArr.push(internationalCountry);

//Set selected country to Other if users country is one of the 'Other' countries
if(otherCountries.indexOf(selectedCountryID) >= 0){
data.selectedIndex = 0;
data.countryName = countryArr[0].name;
}

//set selected country in country picker
var index = 0;
for(var i in countryArr){
//Find position of selected country in array
if (selectedCountryID == countryArr[i].id){
data.selectedIndex = index;
data.countryName = countryArr[i].name;
//gs.addInfoMessage(data.countryName);
}
index++;
}
data.countryArr = countryArr;
data.countryID=selectedCountryID;

})();

Please mark answer as Correct or Helpful based on impact.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Thanks Mate for the quick response 🙂 It worked like a charm..