- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2024 09:10 AM - edited 10-21-2024 09:11 AM
Looks like you almost have it, but in this case obj isn't defined, so you would use the 'array' variable in the for loop, and account for the value of the element named "value" being an array of objects itself. I'm not sure what you were trying to do with the if statements, so I took them out. If they are needed for something you can put back whatever makes sense, and to setValue on a string there is only one parameter, so something more like this:
function onLoad() {
var clientindex = g_service_catalog.parent.getValue('client_index');
var gaFields = new GlideAjax('CLIENTdata');
gaFields.addParam('sysparm_name', 'getDetails');
gaFields.addParam('sysparm_id', clientindex);
gaFields.getXML( showData);
function showData(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert('Answer: ' + answer);
var obj = JSON.parse(answer);
for (var i = 0; i < obj.value.length; i++) {
g_form.addOption('contact_name', obj.value[i].ContactName, obj.value[i].ContactName);
g_form.setValue('relationship', obj.value[i].Relation);
}
}
}
Note that with the 'answer' you are getting, 'array' is an object, not an array, so you I renamed that script variable for future clarity.