krumpirko8888
krumpirko8888

Reputation: 5

Having problem executing ajax callback in Oracle APEX

I have temporally interactive grid (118_ig) in Oracle APEX and I use JavaScript to take numbers IG turn commas into the dot from it along side with ID from item :P188_id . This is done with following script and according to the console log it works alright:

var v_amount = [];

var grid = apex.region("118_ig").widget();
var gridView = grid.interactiveGrid("getViews", "grid");
var model = gridView.model;
var p118_id = apex.item("P118_ID").getValue();

model.forEach(function (record) {
    console.log("Record structure:", record);
    if (record[1] !== undefined) {
        let value= String(record[1]).replace(/,/g, '.');
        if (!isNaN(value)) {
            v_amount.push(value);
        }
    }
});

apex.server.process('split_the_bill_btn_ig_proc', {
        f01: p118_id,
        f02: v_amount
    },
    {
        success: function (data) {
            console.log("Process succeeded:", data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.error("Process failed:", textStatus, errorThrown);
        }
    }
);

and with ajax call back being written in PL/SQL as:

Where string of arrays is formatted to CSV

DECLARE
    l_csv VARCHAR2(32767);
BEGIN
    l_csv := '';
    FOR i IN 1 .. apex_application.g_f02.COUNT LOOP
        IF i > 1 THEN
            l_csv := l_csv || ',';
        END IF;

        l_csv := l_csv || apex_application.g_f02(i);
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('CSV Result: ' || l_csv);

    SPLIT_BILL_PKG.split_the_amount(apex_application.g_f01(1),  SPLIT_BILL_PKG.amounts_type(l_csv));

END;

However, this for some reason isn't working.

I tried everything, googling, looking at scripts from my coworker who swears this is way it should be done. I can't seem to figure out should I use f01 or x01. My coworker says there are rules, but I'm unable to find them.

Manually I can call package using PL/SQL in SQL commands and it works as intended. but when calling it like this it doesn't.

Upvotes: 0

Views: 63

Answers (1)

MOA
MOA

Reputation: 21

Please don't use f01.. anymore. They are depricated! The Codes below should work fine:

JavaScript

var v_amount = [];

var grid = apex.region("118_ig").widget();
var gridView = grid.interactiveGrid("getViews", "grid");
var model = gridView.model;
var p118_id = apex.item("P118_ID").getValue();

model.forEach(function (record) {
    console.log("Record structure:", record);
    if (record[1] !== undefined) {
        let value= String(record[1]).replace(/,/g, '.');
        if (!isNaN(value)) {
            v_amount.push(value);
        }
    }
});

// convert array to string or use other way to generate delimited string
// toString() will give comma separated string
let v_amount_str = v_amount.toString();

apex.server.process('split_the_bill_btn_ig_proc', {
        x01: p118_id,
        x02: v_amount_str
        // ,pageItems: "#P1_DEPTNO,#P1_EMPNO" // page items can be submitted this way
    },
    {
        success: function (data) {
            console.log("Process succeeded:", data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.error("Process failed:", textStatus, errorThrown);
        }
    }
);

PL/SQL (Ajax Callback)

DECLARE
    l_p118_id       VARCHAR2(50)      := apex_application.G_X01; -- your p118_id
    l_v_amount_str  VARCHAR2(4000)    := apex_application.G_X02; -- your v_amount (comma separated)
    l_split_amount  APEX_T_VARCHAR2;
    -- l_csv           VARCHAR2(32767);
BEGIN
    -- p118_id
    dbms_output.PUT_LINE('p118_id: ' || p118_id);

    -- convert v_amount back to table/array (SPLIT/STRING_TO_TABLE)
    l_split_amount := apex_string.SPLIT(l_v_amount_str, ','); -- change delimiter, if other one used

    -- now you can count or loop through it
    --count
    dbms_output.PUT_LINE('l_split_amount.COUNT: ' || l_split_amount.COUNT);

    -- loop through amounts
    FOR i IN 1..l_split_amount.COUNT LOOP
        dbms_output.PUT_LINE('l_split_amount('||i||'): '|| l_split_amount(i));
    END LOOP;

    -- your old codes
    /*
    l_csv := '';
    FOR i IN 1 .. apex_application.g_f02.COUNT LOOP
        IF i > 1 THEN
            l_csv := l_csv || ',';
        END IF;

        l_csv := l_csv || apex_application.g_f02(i);
    END LOOP;
    
    DBMS_OUTPUT.PUT_LINE('CSV Result: ' || l_csv);

    SPLIT_BILL_PKG.split_the_amount(apex_application.g_f01(1),  SPLIT_BILL_PKG.amounts_type(l_csv));
    */
END;

Upvotes: 0

Related Questions