How to check if query and function are executed successfully?

851 Views Asked by At

I have function that takes few arguments then query delete will be executed. Right after that I call another function that will update table with time stamp and user id. The code that I use looks like this:

remote function deleteComment(required string storeID, required string recID) returnFormat="JSON" {
    local.fnResults = structNew();
    local.runProcess = true;

    try {
        if ( !len(arguments.recID) ) {
            local.runProcess = false;
            local.fnResults = {status: 400, message: "Error! Incomplete or invalid data."};
        }

        if ( runProcess ) {
            local.qryParams = {rec_id: {cfsqltype: "cf_sql_numeric", value: "#arguments.recID#"}};
            local.deleteSQL = "DELETE FROM Articles WHERE rec_id = :rec_id";
            queryExecute(deleteSQL, qryParams, {datasource: "#application.datasource#", result: "deleteResult"});
            local.updateResult = updateProfile(arguments.agencyID);

            if ( deleteResult.recordcount && updateResult.recordcount ) {
                local.fnResults = {status: 200, message: "Record successfully removed."};
            } else {
                local.fnResults = {status: 400, message: "Error! Query failed."};
            }
        }
    } catch ( any e) {
        local.fnResults = {status: 400, message: "Error! Please contact your administrator"};
    }

    return fnResults;
}

Here is example of the function updateProfile:

public struct function updateAgencyProfile(required string storeID) {
    try {
        local.qryParams = {
            storeID: {cfsqltype: "cf_sql_numeric", value: "#arguments.storeID#"},
            user_id: client.userid
        };

        local.Profile_SQL = "
            UPDATE profile
            SET last_update = getDate(),
                user_id = :user_id
            WHERE store_id = :storeID
        ";

        queryExecute(Profile_SQL, qryParams, {datasource: "#application.datasource#", result: "updateResult"});

        return updateResult;
    } catch ( any e) {
        return {status: 400};
    }
}

As you can see in my example above delete query is executed first and right after I call public function updateProfile that will update profile table and return query result. I used this block of code to check if both queries are executed successfully:

if ( deleteResult.recordcount && updateResult.recordcount ) {
    local.fnResults = {status: 200, message: "Record successfully removed."};
} else {
    local.fnResults = {status: 400, message: "Error! Query failed."};
}

I'm wondering if that is necessary or I can let try catch block to detect any errors in this function? Also, is this a good fit for stored procedure? I have other functions that will need to call updateProfile to update the records. If you have any questions please let me know.

0

There are 0 best solutions below