- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2024 08:26 AM
Hello, I am trying to pass the contact name into the drop down and auto-populate the Relationship variable depending on the selected Contact Name.
What should be fixed in the onLoad script below?
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 array = JSON.parse(answer);
alert('Array: ' + array);
for (var i = 0; i < obj.fields.length; i++) {
if (obj.fields[i].name == "ContactName") { //contact_name is a select box
g_form.addOption('contact_name', array[i].ContactName, array[i].ContactName);
}
if (obj.fields[i].name == "Relation") { //relationship is s string field
g_form.setValue('relationship', array[i].Relation, array[i].Relation);
}
}
}
}
Answer return:
{"value":[{"UniqueID":"369851","ContactName":"JESSICA","Relation":"Vendor"},{"UniqueID":"789456","ContactName":"ANGELICA","Relation":"Vendor"},{"UniqueID":"951753","ContactName":"DAVID","Relation":"Vendor"},{"UniqueID":"201698","ContactName":"MARGRET","Relation":"Client"}]}
Array return [Object object]
Solved! Go to Solution.
- 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.
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2024 10:07 AM
Thanks @Brad Bowman !
This solved my problem!
Another question, if you don't mind. I'd also like the Relationship field to be updated when the Contact Name changes. How should the onChange script look like?