I’m having trouble capturing data from multiple Interactive Grids in Oracle APEX Oracle APEX 24.2.3 using JavaScript. When I try to save the data, I’m getting undefined values for all records.
Here’s my current JavaScript code:
function getIGData(staticId) {
console.log('Getting data for region:', staticId);
var region = apex.region(staticId);
if (!region) {
console.error('Region not found:', staticId);
return [];
}
var grid = region.widget();
if (!grid) {
console.error('Grid widget not found for region:', staticId);
return [];
}
var view = grid.interactiveGrid("getViews").grid;
var model = view.model;
var records = [];
model.forEach(function(record) {
var r = record;
console.log('Raw record data:', r);
var recordData = {
QUESTION: r.QUESTION,
RESPONSE: r.RESPONSE || r["INITIAL ANSWER"],
COMMENTS: r.COMMENTS,
ACTUAL_ANSWER: r.ACTUAL_ANSWER,
FORECAST_ANSWER: r.FORECAST_ANSWER,
SUBQUESTION_ANSWER: r.SUBQUESTION_ANSWER
};
console.log('Processed record data:', recordData);
if (recordData.QUESTION && recordData.RESPONSE) {
records.push(recordData);
}
});
console.log('Data from ' + staticId + ':', JSON.stringify(records));
return records;
}
function saveAllResponses() {
console.log('saveAllResponses function called');
var allData = [];
var regionIds = ["region-question-1", "region-question-2", "region-question-3", "region-question-4", "region-question-5"];
regionIds.forEach(function(regionId) {
var data = getIGData(regionId);
console.log('Data from ' + regionId + ':', data);
allData = allData.concat(data);
});
console.log('All data:', JSON.stringify(allData));
console.log('P22_ID_RISK:', $v("P22_ID_RISK"));
console.log('P22_SUPPLIER:', $v("P22_SUPPLIER"));
console.log('P22_SUBTYPE:', $v("P22_SUBTYPE"));
console.log('P22_EVALUATOR:', $v("P22_EVALUATOR"));
console.log('P22_SUPPLIER_ID:', $v("P22_SUPPLIER_ID"));
if (allData.length > 0) {
apex.server.process(
"SAVE_ALL_RESPONSES",
{
x01: JSON.stringify(allData),
p_id_risk: $v("P22_ID_RISK"),
p_supplier: $v("P22_SUPPLIER"),
p_subtype: $v("P22_SUBTYPE"),
p_evaluator: $v("P22_EVALUATOR"),
p_supplier_id: $v("P22_SUPPLIER_ID")
},
{
success: function(pData) {
console.log('Server response:', pData);
if (pData && pData.success) {
apex.message.showPageSuccess(pData.message);
} else {
apex.message.showErrors([{
type: "error",
location: ["page"],
message: pData && pData.message ? pData.message : "Unknown error occurred",
unsafe: false
}]);
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.error("AJAX error:", textStatus, errorThrown);
console.log("Response Text:", jqXHR.responseText);
apex.message.showErrors([{
type: "error",
location: ["page"],
message: "Error saving responses: " + errorThrown,
unsafe: false
}]);
},
dataType: "json"
}
);
} else {
console.log("No data to save");
apex.message.showErrors([{
type: "error",
location: ["page"],
message: "No data to save. Please ensure at least one question is answered.",
unsafe: false
}]);
}
}
Also I have an Application Process called SAVE_ALL_RESPONSES
DECLARE
l_data CLOB := wwv_flow.g_x01;
l_id_risk NUMBER := :P22_ID_RISK;
l_supplier VARCHAR2(100) := :P22_SUPPLIER;
l_subtype VARCHAR2(100) := :P22_SUBTYPE;
l_evaluator VARCHAR2(100) := :P22_EVALUATOR;
l_supplier_id NUMBER := :P22_SUPPLIER_ID;
l_success BOOLEAN := TRUE;
l_message VARCHAR2(4000) := 'Responses saved successfully.';
l_rows_updated NUMBER := 0;
BEGIN
apex_debug.message('Received data: ' || l_data);
IF l_data IS NULL OR l_data = '[]' THEN
l_success := FALSE;
l_message := 'No data received from the frontend.';
ELSE
APEX_JSON.PARSE(l_data);
FOR i IN 1..APEX_JSON.GET_COUNT(p_path => '.') LOOP
DECLARE
l_question VARCHAR2(4000) := APEX_JSON.GET_VARCHAR2(p_path => '[%d].QUESTION', p0 => i);
l_answer VARCHAR2(4000) := APEX_JSON.GET_VARCHAR2(p_path => '[%d].ANSWER', p0 => i);
BEGIN
UPDATE RMA_USER_EVALUATIONS
SET ANSWER = l_answer
WHERE ID_RISK = l_id_risk
AND SUPPLIER = l_supplier
AND SUBTYPE = l_subtype
AND QUESTION = l_question;
IF SQL%ROWCOUNT = 0 THEN
-- If no row was updated, it means the row doesn't exist, so we insert it
INSERT INTO RMA_USER_EVALUATIONS (
ID_RISK, SUPPLIER, SUBTYPE, QUESTION, ANSWER,
EVALUATOR, SUPPLIER_ID
) VALUES (
l_id_risk, l_supplier, l_subtype, l_question, l_answer,
l_evaluator, l_supplier_id
);
END IF;
l_rows_updated := l_rows_updated + 1;
EXCEPTION
WHEN OTHERS THEN
l_success := FALSE;
l_message := 'Error saving response for question ' || l_question || ': ' || SQLERRM;
apex_debug.error(l_message);
EXIT;
END;
END LOOP;
IF l_success THEN
COMMIT;
l_message := l_message || ' (' || l_rows_updated || ' rows updated/inserted)';
ELSE
ROLLBACK;
END IF;
END IF;
APEX_JSON.OPEN_OBJECT;
APEX_JSON.WRITE('success', l_success);
APEX_JSON.WRITE('message', l_message);
APEX_JSON.CLOSE_OBJECT;
EXCEPTION
WHEN OTHERS THEN
apex_debug.error('Unexpected error: ' || SQLERRM);
APEX_JSON.OPEN_OBJECT;
APEX_JSON.WRITE('success', FALSE);
APEX_JSON.WRITE('message', 'Unexpected error: ' || SQLERRM);
APEX_JSON.CLOSE_OBJECT;
END;
My setup:
- I have 5 Interactive Grids on my page, with IDs: region-question-1, region-question-2, region-question-3, region-question-4, region-question-5
- Each grid has columns: QUESTION, RESPONSE (or INITIAL ANSWER), COMMENTS, ACTUAL_ANSWER, FORECAST_ANSWER, SUBQUESTION_ANSWER
- I’m using a Dynamic Action on a button click to trigger the save function
Expected behavior:
The function should collect data from all grids and send it to the server for processing.
Actual behavior:
The function is not capturing any data from the grids. All record data is coming back as undefined.
So the region 1 should insert the answers in the rows where the question 1 in the table is:
ID_RISK SUBTYPE QUESTION ANSWER COMMENTS SUPPLIER ID SUPPLIER_ID IMPACT ACTUAL_ANSWER FORECAST_ANSWER IMPACT_ACTUAL IMPACT_FORECAST COMMENT_WHEN_CLOSED USER_CLOSED SUBQUESTION_ANSWER ADDITIONAL_INPUT
643 Tropical Storm / Hurricane 1. Impact on operations/production GG Cables and Wires Mexico 881 22352710
643 Tropical Storm / Hurricane 2. Impact on deliveries for BMW GG Cables and Wires Mexico 882 22352710
643 Tropical Storm / Hurricane 3. Status for infrastructure or equipment compromised GG Cables and Wires Mexico 883 22352710
643 Tropical Storm / Hurricane 4. Effective countermeasures are in place to mitigate the risk GG Cables and Wires Mexico 884 22352710
643 Tropical Storm / Hurricane 5. Estimated time for restoration of operations GG Cables and Wires Mexico 885 22352710
So if the user answer like this:example of answer
at the end the table in the DB should look like enter image description here