Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Calculated fields in 6.0 CE

  1. #1
    edlentz is offline Senior Member
    Join Date
    Nov 2010
    Posts
    45

    Default Calculated fields in 6.0 CE

    I want to be able to take a field that is already calculated and divide it by half and insert it into another field. I have a quote module that totals the quote amount. I want to take the total and divide it in half for a down payment amount. Can someone show me how to do this?

    Thanks

  2. #2
    davidboris is offline Sugar Community Member
    Join Date
    May 2010
    Posts
    1,113

    Default Re: Calculated fields in 6.0 CE

    Hello,

    You can write a before_save logic hook. And put code something like,

    PHP Code:
    $bean-><FINAL_FIELD_NAME> = $bean-><FIELD_NAME_TO_DIVIDE_BY_2> / 2
    Thumbs up.

    Skype ID - david__boris

    SugarForge Projects:

    WYSIWYG now in studio!(Version 1.1 is out now!)

    Sugar Feeds on your personalized home pages like iGoogle, My Yahoo!, etc.

    Fab Tools! > Dashlet Not Followed Opportunities for past six Months

  3. #3
    edlentz is offline Senior Member
    Join Date
    Nov 2010
    Posts
    45

    Default Re: Calculated fields in 6.0 CE

    Hi David

    I see what you are doing there. But I am new to this and in the AOS module I want to work I can't find where to use that code. Can you help me with "logic hooks" I am not sure where to go to make this work

    Thanks

    Ed

  4. #4
    edlentz is offline Senior Member
    Join Date
    Nov 2010
    Posts
    45

    Default Re: Calculated fields in 6.0 CE

    Well, looking around I was able to come up with this:
    Code:
    <?php
    $hook_version = 1;
    $hook_array = Array();
    $hook_array['before_save'] = Array(1,'$bean-><downpayment_c> = $bean-><total_amount> / 2');
    
    ?>
    I put this in a logic_hooks.php file and into the root of the module.
    So this does nothing at this point.

    What have I got wrong?

    Thanks

  5. #5
    davidboris is offline Sugar Community Member
    Join Date
    May 2010
    Posts
    1,113

    Default Re: Calculated fields in 6.0 CE

    Hello,

    You should write something like,
    PHP Code:
    $hook_version 1;
    $hook_array = Array();
    $hook_array['before_save'] = Array(1,'Calculating field''custom/modules/<YOUR_MODULE_NAME>/<ANYFILE>.php','calculate_field_class''calculate_field_function'); 
    In that <ANYFILE>.php, must be located as mentioned above, write following code

    PHP Code:
    Class calculate_field_class
    {
     function 
    calculate_field_function()
      {
          
    $bean->downpayment_c $bean->total_amount 2;
       }

    NOW, change every variables here, <YOUR_MODULE_NAME>, <ANYFILE>.
    Thumbs up.

    Skype ID - david__boris

    SugarForge Projects:

    WYSIWYG now in studio!(Version 1.1 is out now!)

    Sugar Feeds on your personalized home pages like iGoogle, My Yahoo!, etc.

    Fab Tools! > Dashlet Not Followed Opportunities for past six Months

  6. #6
    edlentz is offline Senior Member
    Join Date
    Nov 2010
    Posts
    45

    Default Re: Calculated fields in 6.0 CE

    I think I havre it now. I'll try it and report back

    Thanks!

  7. #7
    edlentz is offline Senior Member
    Join Date
    Nov 2010
    Posts
    45

    Default Re: Calculated fields in 6.0 CE

    OK so here's what I have:

    here is my logic_hooks.php file
    Code:
    <?php
    $hook_version = 1;
    $hook_array = Array();
    $hook_array['before_save'] = Array(1,'Calculating field', 'custom/modules/AOS_Quotes/downpaymentcalc.php','calculate_field_class', 'calculate_field_function');
     ?>
    In the downpaymentcalc.php
    Code:
    <?php
    Class calculate_field_class
    {
     function calculate_field_function()
      {
          $bean->downpayment_c = $bean->total_amount / 2;
       }
    }
     ?>
    Both are in the /custom/modules/AOS_Quotes directory.

    Will this work on existing DB entries? Or do I need to start from a new Quote?

    Thanks

  8. #8
    davidboris is offline Sugar Community Member
    Join Date
    May 2010
    Posts
    1,113

    Default Re: Calculated fields in 6.0 CE

    Hello,

    For old database records, you will need to run following query in database directly(Take backup of custom table, to avoid any mishap.),

    PHP Code:
    UPDATE CUSTOM_TABLE_NAME c 
    INNER JOIN TABLE_NAME m ON m
    .id c.id_c 
    SET c
    .downpayment_c = (m.total_amount 2WHERE m.deleted 0
    Change the custom_table_name and table_name with respective table names.

    Logic hook will work when you edit a record or create new one.
    Thumbs up.

    Skype ID - david__boris

    SugarForge Projects:

    WYSIWYG now in studio!(Version 1.1 is out now!)

    Sugar Feeds on your personalized home pages like iGoogle, My Yahoo!, etc.

    Fab Tools! > Dashlet Not Followed Opportunities for past six Months

  9. #9
    edlentz is offline Senior Member
    Join Date
    Nov 2010
    Posts
    45

    Default Re: Calculated fields in 6.0 CE

    Hi David,

    Is there a way to stop a Logic Hook from working when a module is built? The reason I ask is that this isn't working. I have checked over and over and don't see any typos in my field entries.

    Any suggestions?

    thanks for your help

  10. #10
    davidboris is offline Sugar Community Member
    Join Date
    May 2010
    Posts
    1,113

    Default Re: Calculated fields in 6.0 CE

    Hello,

    Paste the code you have implemented, with the path you have put the files at, etc.
    Thumbs up.

    Skype ID - david__boris

    SugarForge Projects:

    WYSIWYG now in studio!(Version 1.1 is out now!)

    Sugar Feeds on your personalized home pages like iGoogle, My Yahoo!, etc.

    Fab Tools! > Dashlet Not Followed Opportunities for past six Months

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calculated Fields
    By alexbloom in forum Help
    Replies: 8
    Last Post: 2010-01-19, 07:31 PM
  2. Calculated Fields
    By cbeagle in forum Developer Help
    Replies: 7
    Last Post: 2009-10-15, 08:33 AM
  3. Creating Fields - calculated fields and 'related' field
    By gipps in forum General Discussion
    Replies: 0
    Last Post: 2007-07-21, 10:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •