System property value example: (YourSystemProperty)
1 = India,
2 = China
Script Include:
YourMethodName: function() {
var propValue = gs.getProperty('YourSystemProperty')
var codes = [];
//gs.info(propValue + '---propValue');
if (propValue) {
var pairs = propValue.split(',');
pairs.forEach(function(pair) {
var parts = pair.split('=');
if (parts.length == 2) {
var order = parts[0].trim();
var value = parts[1].trim();
codes.push({
label: value,
value: value,
order: parseInt(order)
});
}
});
}
// Sort the codes array by order (numerically or as strings)
codes.sort(function(a, b) {
return a.order > b.order ? 1 : (a.order < b.order ? -1 : 0);
});
//gs.info(JSON.stringify(codes) + '---JSON.stringify(codes)');
return JSON.stringify(codes);
},
Client script:
function onLoad() {
// Call the Script Include
var ga = new GlideAjax('YourScriptInclude');
ga.addParam('sysparm_name', 'YourMethodName');
ga.getXMLAnswer(function(response) {
var codes = JSON.parse(response);
console.log(codes);
var fieldName = 'yourFieldName'; // Replace with the actual field name
// Clear existing choices
//g_form.clearOptions(fieldName);
// Add choices from the system property
codes.forEach(function(code) {
g_form.addOption(fieldName, code.value, code.label);
});
});
}