- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 12:36 AM
Hello, everyone. Can someone show me where the problem is while receiving the Parsed value?
This is the response.
Received answer: {"httpStatus":200,"result":[{"sys_id":"7c1b7d7fdb858d9096710149f49619f3","source":"Update Set: Standard 1"}]}
The catalog client script:
function onSubmit() {
//Type appropriate comment here, and begin script below
var catItemName = g_form.getValue('catalog_item');
//g_form.setValue('catalog_item_us_path', catSysID);
alert("catItemName: " + catItemName); // Log the catalog item name
var ga = new GlideAjax('RErequestAPI');
ga.addParam('sysparm_name', 'getUSpath');
ga.addParam('sysparm_cat_item_name', catItemName);
ga.getXMLAnswer(getCatItemsOutput);
}
function getCatItemsOutput(answer) {
// Log the received answer
alert("Received answer: " + answer);
if (answer) {
try {
var responseJSON = JSON.parse(answer);
/** Check if the response contains the expected data
for(var i=0; i<responseJSON.result.length; i++){
if (responseJSON.result[i].name.equals("source")) {
// Set the value of the 'catalog_item_us_path' variable
g_form.setValue('catalog_item_us_path', responseJSON.result[i].display_value);
alert(responseJSON[0].source);
} else {
// Display an error message if the expected data is not found
g_form.addErrorMessage("Pfad zum US nicht gefunden");
}
}*/
// Check if the response contains the expected data and has results
if (responseJSON && responseJSON.result && responseJSON.result.length > 0) {
// Access the first result directly, as the example payload only contains one
var result = responseJSON.result[0];
// Check if the 'source' property exists
if (result.hasOwnProperty('source')) {
// Set the value of the 'catalog_item_us_path' variable
g_form.setValue('catalog_item_us_path', result.source); // Use 'source' value directly
} else {
// Display an error message if the 'source' property is not found
g_form.addErrorMessage("Pfad zum US nicht gefunden");
}
} else {
// Handle cases where the response is invalid or empty
g_form.addErrorMessage("Ungültige Antwort vom Server erhalten.");
}
} catch (e) {
// Handle JSON parsing errors
g_form.addErrorMessage("Fehler beim Verarbeiten der Antwort: " + e.message);
}
} else {
// Display an error message if the answer is empty or null
g_form.addErrorMessage("Pfad zum US nicht gefunden");
}
}
I've tried multiple versions and commented them out. I want to parse the result and store only the part associated with the source in a variable.
Thank you for any help.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 01:46 AM
The main problem is you are using an Ajax call within an onSubmit Catalog Client Script, which is not a compatible combination. The item will be submitted before the result comes back, so the "getCatItemsOutput" function will likely never run (or, at least, should not).
When/how are you getting that result? Is it really coming back on the submit? If you put in an "alert" as the last possible statement in the "getCatItemsOutput" function, does it run? If you hard-code a value in the "onSubmit" part, does it work?
What is the use case for the item? Should the submit be cancelled if there is an error? There is no provision for that at the moment.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 12:51 AM
@JohnDF The code looks okay to me, are you not able to see value of
result.source
in the catalog_item_us_path field?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 01:11 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 01:58 AM - edited 10-01-2024 02:14 AM
Make it synchronous call otherwise it will not hold return information in the variable and use
if (responseJSON && responseJSON.result && responseJSON.result.length > 0) {
var result = responseJSON.result[0];
if (result.source) {
g_form.setValue('catalog_item_us_path', result.source);
} else {
g_form.addErrorMessage("Pfad zum US nicht gefunden");
}
} else {
g_form.addErrorMessage("Ungültige Antwort vom Server erhalten.");
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 02:06 AM
@JohnDF Found the issue, GlideAjax is an asynchronous call and by the time you get a response from the server side the form is already submitted. You can use the following script to eliminate this issue.
function onSubmit() {
if (g_scratchpad.isFormValid){
return true;
}
// Get the catalog item name from the form
var catItemName = g_form.getValue('catalog_item');
alert("catItemName: " + catItemName); // Log the catalog item name
var actionName = g_form.getActionName();
// Use GlideAjax to call the script include
var ga = new GlideAjax('RErequestAPI');
ga.addParam('sysparm_name', 'getUSpath');
ga.addParam('sysparm_cat_item_name', catItemName);
// Get the response and pass it to the callback function
ga.getXMLAnswer(getCatItemsOutput);
}
function getCatItemsOutput(answer) {
// Log the received answer
alert("Received answer: " + answer);
if (answer) {
try {
// Parse the JSON response
var responseJSON = JSON.parse(answer);
// Check if the response contains the expected data and has results
if (responseJSON && responseJSON.result && responseJSON.result.length > 0) {
// Access the first result directly
var result = responseJSON.result[0];
// Check if the 'source' property exists
if (result.hasOwnProperty('source')) {
// Set the value of the 'catalog_item_us_path' variable
g_form.setValue('catalog_item_us_path', result.source); // Use 'source' value directly
alert("Source: " + result.source); // Log the source value
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
} else {
// Display an error message if the 'source' property is not found
g_form.addErrorMessage("Pfad zum US nicht gefunden");
}
} else {
// Handle cases where the response is invalid or empty
g_form.addErrorMessage("Ungültige Antwort vom Server erhalten.");
}
} catch (e) {
// Handle JSON parsing errors
g_form.addErrorMessage("Fehler beim Verarbeiten der Antwort: " + e.message);
}
} else {
// Display an error message if the answer is empty or null
g_form.addErrorMessage("Pfad zum US nicht gefunden");
}
return false;
}
Hope this works for you.