Oracle APEX 24.1 | Oracle DB 19c
Hi, I'm editing an older APEX app that was apparently copying Page Items to a new record in an IG. It did so like this:
1. A certain page item, we'll call it PX_ITEM, had a dynamic action that fired on PX_ITEM changing
2. The action executed JavaScript code:
apex.region('ig_static_id')
.widget()
.interactiveGrid('getActions')
.invoke("row-add-row");
let region = 'ig_static_id';
let args = [
['NAME', 'PX_NAME'],
['AMT', 'PX_AMT']
];
setTimeout(copyToIg(region, args), 5000);
3. The function copyToIg was in the “Function and Global Variable Declaration”
function copyToIg(targetRegion, fieldDetails) {
const grid = apex.region(targetRegion).call('getViews', 'grid');
const model = grid.model;
const nextId = model._nextInsertId;
const recordId = ('t' + (nextId - 1).toString());
// console.log('Next ID: ' + nextId);
// console.log('Record ID: ' + recordId);
for (let i = 0; i < fieldDetails.length; i++){
// console.log(fieldDetails[i][0]);
// console.log(apex.item(fieldDetails[i][1]).getValue());
model.setRecordValue(recordId, fieldDetails[i][0], apex.item(fieldDetails[i][1]).getValue());
}
}
Now, I was running into problems and at first I thought it was the index, so I threw in those console.log statements for the fieldDetails variable. Those worked, as in, the expected values printed.
I went through several iterations, and I eventually realized the problem was with the recordId variable. When I would console.log both nextId and recordId, I get undefined and tNaN, respectively. The fact that nextId is undefined is a problem, so I started looking into that.
What I found is that calling invoke("row-add-row") is asynchronous, or at least I think so based on the call to copyToIg being wrapped in setTimeout. But 5 whole seconds seems like a lot. Not only does it seem like a lot, but I'm not sure it's doing anything as I get an error immediately after the change event fires (at least immediately to my human perception - definitely not 5 seconds…).
Because of all this, I thought maybe a different approach is warranted.
I tried subscribing to the change event of the IG like this:
const region = apex.region("ig_static_id");
const model = region.widget().interactiveGrid("getViews", "grid").model;
model.subscribe( {
onChange: function( changeType, change ) {
if (changeType === 'insert') {
model.setValue(change.recordId, 'NAME', apex.item('PX_NAME').getValue());
model.setValue(change.recordId, 'AMT', apex.item('PX_AMT').getValue());
}
}
});
But alas, this doesn't seem to do anything… A row is created in my IG, and then my cursor is put in the NAME field of the IG, but the values from the corresponding Page Items do not populate. However, I'm not getting an error now.
So after a couple hours trying to figure this out, I'm reaching out to ask for suggestions / tips on how to do this. I know there is an IG cookbook out there, but I'm not sure where to look in it (but I am looking now). Anyways, any pointers in the right direction would be greatly appreciated. Thanks.