- 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 05:33 AM
@JohnDF Did you try this solution? It should work for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 12:54 AM
Hello @JohnDF ,
The problem seems to be with how you are parsing and accessing the values from the JSON response. Your current code is mostly correct, but I'll walk you through the necessary changes and provide a clean script.
Issue:
You're trying to extract the `source` value from the response, but there are some commented-out lines and structural issues. I'll refine the script for you.
Suggested Solution:
1. Check JSON Structure:
Make sure to properly parse the JSON response and access the `source` field correctly.
2. Update the Catalog Client Script:
Here's a cleaned-up version of your code with improvements for handling the JSON response and setting the correct value to the form:
function onSubmit() {
// Get the catalog item name from the form
var catItemName = g_form.getValue('catalog_item');
alert("catItemName: " + catItemName); // Log the catalog item name
// 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
} 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");
}
}
Best regards,
Siddhesh Jadhav
Please test this setup and see if it works. If it does, don't forget to mark this as helpful and accept the answer!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 01:11 AM
@Siddhesh Jadhav thanks for reply. I get the right alert:
Source: Update Set: Default 1
But my variable keeps empty:
What could be the error?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 01:23 AM
Hi @JohnDF
Please try using g_form.setValue('catalog_item_us_path', result.source); to set the value. It should work since source is the correct property from the JSON response
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2024 01:32 AM
Replace the line inside the `getCatItemsOutput` function where you're currently setting the value.
Specifically, replace:
g_form.setValue('catalog_item_us_path', responseJSON.result[i].display_value);
with:
g_form.setValue('catalog_item_us_path', result.source);