Math/Update field after record is saved.

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
jhart
Posts: 12
Joined: 2018-07-21 20:31

Math/Update field after record is saved.

Post by jhart » 2018-07-23 17:39

I have a table "Table1" In this table is a field with a numeric value. Units. Current Value is 100. I have another table "Table2" that is a child of Table one. I have a field in the table called "UnitsUsed". I need to have the field in "Table1" updated and subtract the amount in "UnitsUsed" and the display the new amount.

Not sure how to start. Can someone assist me please?

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Math/Update field after record is saved.

Post by pbottcher » 2018-07-23 19:02

Hi,

you can do this in the hooks/<Table2>.php file. There you will find the functions <table2>_after_insert and <table2>_bevor_update and <table2>_after_update and <table2>_after_delete.

The data array has the values from your detailview. So update units in table1 via the sqlvalue function.

Something like

sqlvalue("UPDATE table1 set Units=Units-".$data['UnitsUsed']." where ### PUT HERE your matching between table1 and table2 ### = ". $data['' ## PUT here your table2 reference to table1 from your form ###'] );

for the insert.

Make sure your logic covers all aspects which can occure, especially if you allow updates.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

jhart
Posts: 12
Joined: 2018-07-21 20:31

Re: Math/Update field after record is saved.

Post by jhart » 2018-07-23 22:01

Ok so here is a screen shot of my two lists. Here is a copy of the command I entered into the Hooks file:

function BHISNotes_after_insert($data, $memberInfo, &$args){

sqlvalue("UPDATE Clients set BHISUnits=BHISUnits-".$data['UnitsUsed']." where client = ". $data['fullname'] );
return TRUE;
}

After I save the screen for the data entry seems to hang and when I click the X no math has been performed.
Attachments
2018-07-23 (4).png
2018-07-23 (4).png (17.42 KiB) Viewed 6015 times

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Math/Update field after record is saved.

Post by pbottcher » 2018-07-24 06:02

Hi,
can you try

Code: Select all

sqlvalue("UPDATE Clients set BHISUnits=BHISUnits-".$data['UnitsUsed']." where id = ". $data['Client'] ); 

$data['fullname'] is not available.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

jhart
Posts: 12
Joined: 2018-07-21 20:31

Re: Math/Update field after record is saved.

Post by jhart » 2018-07-24 18:19

Yes this work great. Next small but important question - How can I make the BHISUnits field in the first table read only to all users but admins? And if this is possible would it affect this hook from updating the field value?

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Math/Update field after record is saved.

Post by pbottcher » 2018-07-24 21:24

Hi,

you can use this in the hooks/Clients.php

Code: Select all

function Clients_dv($selectedID, $memberInfo, &$html, &$args){
                                
                                /* current user is not an admin? */
             if($memberinfo['group'] != 'Admins'){
                    $html .= <<<EOC
                                        <script>
                                              \$j(function(){
                                              \$j('#BHISUnits').prop('disabled', true);
                                              })
                                        </script>
EOC;
             }
And it does not affect the other hook.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

jhart
Posts: 12
Joined: 2018-07-21 20:31

Re: Math/Update field after record is saved.

Post by jhart » 2018-07-24 21:32

I added the code as shown and when I browse to the page I get the following Error:

This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500

Attached is my clients.php
Attachments
2018-07-24.png
2018-07-24.png (64.66 KiB) Viewed 5983 times

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Math/Update field after record is saved.

Post by pbottcher » 2018-07-25 05:29

Hi,

you need to add an addtional closing bracket } to end the Clinets_dv function.
So after EOC; you need } }
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

jhart
Posts: 12
Joined: 2018-07-21 20:31

Re: Math/Update field after record is saved.

Post by jhart » 2018-07-25 10:49

I did catch that. Does it matter that the field BHISUnits is an Integer? " \$j('#BHISUnits').prop('disabled', true); "

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Math/Update field after record is saved.

Post by pbottcher » 2018-07-25 11:16

No, why?
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

jhart
Posts: 12
Joined: 2018-07-21 20:31

Re: Math/Update field after record is saved.

Post by jhart » 2018-07-25 11:17

Well I added the closing bracket but I am still getting the same error
Attachments
2018-07-25.png
2018-07-25.png (116.66 KiB) Viewed 5958 times

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Math/Update field after record is saved.

Post by pbottcher » 2018-07-25 12:28

Hi,
I see the function definition 2 times in your screenshot, remove one pls.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

jhart
Posts: 12
Joined: 2018-07-21 20:31

Re: Math/Update field after record is saved.

Post by jhart » 2018-07-25 12:35

OK, I am so sorry this isn't working, I am sure it is something I must be doing. I have deleted the section and re added. Attached is the whole hooks file. Thank you for your help and patience. I am just learning and appreciate your help!
Attachments
2018-07-25 (1).png
2018-07-25 (1).png (201.75 KiB) Viewed 5954 times

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Math/Update field after record is saved.

Post by pbottcher » 2018-07-25 12:50

Sorry, but you still have the function defintion twice.

I re-add the code here as it should be:

Code: Select all

function Clients_dv($selectedID, $memberInfo, &$html, &$args){
                                
                                /* current user is not an admin? */
             if($memberinfo['group'] != 'Admins'){
                    $html .= <<<EOC
                                        <script>
                                              \$j(function(){
                                              \$j('#BHISUnits').prop('disabled', true);
                                              })
                                        </script>
EOC;
             }
}
make sure function Clients_dv exists only once.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

Post Reply