Calling Libraries from Functions

I have several forms, each having multiple pages. For example, my StudyGroups form has a Members page showing members in each study group. there may be other pages. each page is powered by a DataView. They use join tables and selected tables.

These forms call InitSelectedTables function which, using saved metadata, refreshes the Selected… table with defaults stored in the respective join table. This function makes use of various libraries. So far, everything is working ok.

Now one of my forms needs to run code to set some form fields. For example, my GenerateEmails form, in addition to running InitSelectedTables needs to run a library called GetEmailDefaults, which will read the bound fields DefaultSubject and DefaultBody (they are hidden on the form) and use those values to update the unbound fields Subject and Body.

At first I thought I’d put qualifying code in InitSelectedTables function such as “if this form is ‘GenerateEmails’, then call this library. But this seemed cumbersome, and the function will get bigger and messier.

So I tried copying this function code and making it into a library instead. Then created a new function called FormShow, which would call the InitSelectedTablesLib library and optionally also call the GetEmailDefaults library.

Then I changed the OnShow event of one form to instead run the FormShow function. So now I get this:

Am I trying to do too much, and should I just put special conditions in the InitSelectedTable function to sometimes call the library? Thanks…

Hi Ron,

Thank you for bringing this scenario to our attention.

Could you please send an email to elton@five.co with the function you are attempting to split into libraries, the library itself, and the related events and forms where you are encountering this issue?

If possible, it would also be helpful if you could share your application for further investigation.

Thank you.

Regard,
Elton S

Hi Elton,

Thanks again for your help. I sent you an email explaining my questions. I tried to export an FDF file to put out on my OneDrive folder for you, but it no longer lets me export. When I click the button, the five logo spins for a while, then I get a popup messge saying “Bad Gateway”. Once I click that message away, it automatically logs me out of my Five account. What is wrong here?

Hi Ron,

Thank you, I have received your email.

I will start to analyse the function. Meanwhile, would be able to create a temporary user and send the credentials to my email, so I could try to export the application from my side.

I believe it would be easier to analyse this error using the same version of your app.

Regards,
Elton S

Hi Ron,

Giving the image previously provided, I will only be able to properly analyse it when I have a copy of your application (As I need the entire context of your setup to simulate it).

Regarding split your function into a library, I could not identify any issue with your function, as I am not able to test it.

However, I created an example of how you could split your code. I hope this is in line with what you are looking for (Bear in mind that I did not test your code as it won’t work Isolated):

————————————————–
General function to be called and decide which library to redirect the logic:

function FormShow(five, context, result)  {

switch (five.getVariable(‘MyForm’)) {

case 'GenerateEmails':

    LoadEmailDefaults(five, context);

    break;

case 'Page2':

    Library2(five, context, result);

    break;

case 'Page3':

    Library3(five, context, result);

    break;

case 'Page4':

    Library4(five, context, result);

    break;  

default:

    return five.createError(result, 'Option Provided is not valid');  
}

return five.success(result);

}

———————————-
Example of the library called by the function ‘FormShow’ when myForm is equal to ‘GenerateEmails‘.

function LoadEmailDefaults(five, context)  {

//Define all the Client side function that call the server function.

const \_five = five;



// save variables MyForm, MyTable, MyKey, IsSetupForm

SetFormVariables(five,

    null,

    null);



// get email defaults if proper form

myForm = five.getVariable('MyForm');



if (myForm ===

    'GenerateEmails') {

    LoadEmailDefaults(five);



}



// loop thru subforms to update selected tables



const result = GetSubforms(five);



if (result.errOK) {



    const subforms = result.context;



    // five.log('subforms: ' + JSON.stringify(subforms));



    for (const subform of subforms) {

        // five.log('subform: ' + subform);



        // get context for server function



        const ctxResult = GetSelectedJoinTableContext(five, subform);



        //five.log(\`InitSelectedTables: errOK=${ctxResult.errOK}\`);



        if (ctxResult.errOK) {



            const myParms = ctxResult.context;



            // five.log(\`InitSelectedTables: got context. subform=${subform}, myParms=${JSON.stringify(myParms)}\`);



            five.executeFunction('InitSelectedTableServer', myParms,

                null,

                '',

                '',

                function(serverResult) {



                    if (serverResult.serverResponse.errorCode ===

                        'ErrErrorOk') {

                        return;

                    }

                    let functionMessage = serverResult.serverResponse.results;

                    if (functionMessage !==

                        '') {

                        \_five.showMessage(functionMessage);

                    }

                }

            );



        } else {

            five.showMessage(\`error getting join table context: ${ctxResult.message}\`);

        }

        // got join table context

    }

    // for

} else {

    five.showMessage(\`error getting subforms: ${result.message}\`);

}




return five.success(result);
}

Regards,
Elton S

I was able to obtain my objective without splitting functions into libraries, so let’s cancel this topic for now. Thanks…

1 Like