- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 01:09 AM - edited 10-22-2024 01:18 AM
Hello all,
The API retrieved the following value, and the user can select Contact Name from the drop down.
{"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"}]}
And my question, how can the onChange script be scripted if the Relation (variable type = string) should be updated depending on the Contact Name (variable type = Select box)?
The current onload script:
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);
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 03:39 AM - edited 10-22-2024 04:02 AM
Hello @Sanjay191 ,
Edit: I found the issue.
Thanks
The corrected script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
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++) {
if (obj[i].ContactName === newValue)
g_form.setValue('relationship', obj.value[i].Relation);
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 01:37 AM - edited 10-22-2024 01:59 AM
Not sure what you're trying to do.
Seems like you have a script include named CLIENTdata that returns a json Contact Name in your question.
This script include in called onLoad so the select box will be filled when the form is loaded. Not sure why you have g_form.setValue('relationship', obj.value[i].Relation); in your onLoad in the for loop.
If you want to change the value of field relationship when field contact_name is changed, create a onChange client script on variable contact_name. Use the "newValue" to get the selected value. Call the CLIENTdata to get the json once again in the onChange and get the matching Relation value.
EDIT:Probably would be better to add a function in Script Include to return only the corresponding relationship value given "newValue" of contact_name as an argument to the function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 01:41 AM
Hello @Hitoshi Ozawa The API is returning all of the values. Do you have a sample script that retrieves only the Relation based on the specified Contact Name? For example, when David is selected, Relation should appear as a vendor.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 01:43 AM
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2024 03:39 AM - edited 10-22-2024 04:02 AM
Hello @Sanjay191 ,
Edit: I found the issue.
Thanks
The corrected script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
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++) {
if (obj[i].ContactName === newValue)
g_form.setValue('relationship', obj.value[i].Relation);
}
}
}