LogicHook to calculate field

Hi all,
I need to show the sum of some fields taken from another module in a field in the current module.
It is not a problem to query and find the value but which logic hook is recommended for this?

It would be advisable to update the field at each listview … can I use after_retrieve?
I see there are contraindications.
In order to prevent loop, does it make sense to use this hook to update the db directly instead of trying to save the field?

Thanks so much for the support.

@2di7

  1. If you want save data you should use ‘before_save’
  2. If you want only to show data you should use ‘process_record’. It works for all views include detail and edit.
1 Like

Thank you for your answer.
Ok, I would have preferred to save in the db but I need this calculation to be done on listview or detailview. So I’ll choose process_record.

But a practical question:
Can I use something like $id = $ bean-> id
to retrieve the fields and suite executes this hook for each line?

Thanks again for the clarification

@2di7
The hook ‘process_record’ called for each record of a module separately.

Can I suggest an option? You can decide your task if you will use special parametr ‘function’ for a filed. As an example:
I make the field ‘age_years’. The field is calculating the age of contact.

  1. The file - custom/Extension/modules/Contacts/Ext/Vardefs/age_years.php:
<?php
$dictionary['Contact']['fields']['age_years'] = 
  array (
    'name' => 'age_years',
    'vname' => 'LBL_AGE_YEARS',
    'type' => 'int',
    'len' => '3',
    'comment' => '',
    'importable' => true,
    'function' => array(
        'name' => 'get_age_years', 
        'returns' => 'html', 
        'onListView' => 'true',
         'include' => 'custom/modules/Contacts/Contacts_fields_functions.php'
    ),
    'studio' => 'false',
    'source' => 'non-db',
    'inline_edit' => 0,
  );
 ?>
  1. The file -
    custom/modules/Contacts/Contacts_fields_functions.php
<?php
function get_age_years($focus, $field, $value, $view) {
    if ($view == 'ListView') {
        if (isset ($focus['ID']) && !empty($focus['ID'])) {
            $contact = new Contact();
            $contact->retrieve($focus['ID']);
            if (!empty($contact->birthdate)) {
                $age_years=ageYears($contact->birthdate);
            }else{
                $age_years = '-';
            }
            return $age_years;
        } else {
            return "?";
        }
    } elseif ($view == 'DetailView') {
            if (!empty($focus->birthdate)) {
                $age_years=ageYears($focus->birthdate);
            }else{
                $age_years = '-';
            }
            return $age_years;
    }
}
function ageYears ($birthdate){
    global $timedate;
    $birthdate_date = date_create_from_format($timedate->get_date_format(),$birthdate);
    $age_years = date("Y") - $birthdate_date->format("Y");
    if (date("md") < $birthdate_date->format("md")) {
        $age_years = $age_years - 1;
    }
    if ($age_years < 0 || $age_years > 999){
        $age_years = '-';
    }
    return $age_years;
}
2 Likes

Thanks p.konetskiy it’s a great tip!

Hi all

I came across this post and tried to implement it without success. Initially tried it on SCRM8.1.3 and then on SCRM7.12.7, still no success.

If I understand it correctly,

  1. I create the field ‘age_years’ in studio (integer, max size 3) and place it on Detail View.

  2. Create the file - custom/Extension/modules/Contacts/Ext/Vardefs/age_years.php

  3. Create the file -custom/modules/Contacts/Contacts_fields_functions.php

The field is obviously there, but no age is viewed in the field.

Can someone help me out here please.

Thanks

Hi @TerryL !
No need to create a field in the studio. (I didn’t write about it)
Creating a field in the studio takes precedence over manual creation. Item 2 of your post creates the field manually.
I recommend:

  1. Remove the field in the studio
  2. Run “Quick Repair and Recovery” two times.

Thanks for your reply p.konetskiy,

I did think that was the case, which I had tried previously. Anyway, I removed the field in Studio and QRR twice, but I get the same result… nothing.

Am I missing something here?

I don’t understand where the field would appear on the ‘detail View’ layout.? If the field does not appear in the fields list in Studio, how do I place it where I want it to be on the layout.?

The following file was created ok.

Thank you for your help.

Aha! For any newbies like me that are following this post, There is a step 4 & 5…

  1. Quick repair and Recovery - twice maybe.

  2. Modify the file -custom/modules/Contacts/metadata/detailviewdefs.php like so…


    I had previously already added the birthdate field using studio.

  3. Quick repair and Recovery again. Done! Works on SuiteCRM 7.12.7

Unfortunately I cannot get this to work on SuiteCRM 8.1.3, which is where I really want this.

p.konetskiy, can you help here?

1 Like

@TerryL

  1. You should have 2 files only like in the post:
    LogicHook to calculate field - #4 by p.konetskiy
  2. Remove the field “age_years_c” in Studio. If you can’t remove the field in Studio you can remove it manually:
  • Remove the file “custom/Extension/modules/Contacts/Ext/Vardefs/_override_sugarfield_age_years_c.php”;
  • Remove the record of the field “age_years_c” in table “fields_meta_data” of your database;
  • Do QRR;
  • Check the file “custom/modules/Contacts/Ext/Vardefs/vardefs.ext.php” . It shouldn’t have data about the field “age_years_c”.

Hi p.konetskiy,

Ok, so I did exactly as you said in your last post. It didn’t work. So I started again and did exactly as you said - it still does not work in SCRM-8.

In your last post you said:

  1. You should have 2 files only like in the post:
    LogicHook to calculate field - #4 by p.konetskiy

I assume I do still need to enter the metadata details for field ‘age_years’ in custom/modules/Contacts/metadata/detailviewdefs.php as per my earlier post (step5). If I don’t do this I get no field at all on Contacts Detail view…

SO at this stage this is what I have on the Contact Detail View;

As you can see, no age is calculated. Interestingly, when I go to ‘Edit View’ the field is also shown even though the editview metadata was not altered.

Any other suggestions please?
I am appreciating your help with this.

Thanks

@TerryL
SuiteCRM 8 don’t support key ‘function’ for field. It will be work in SuiteCRM 7 only.

OK, thanks p.konetskiy.

To know this earlier would have saved me many many hours, but perhaps you did not know either.

It seems to me that SuiteCRM 8 has many many shortfalls. Everything I try to implement ends with a roadblock. I now wish I had started with SuiteCRM 7.

Thanks for your replies.