How to update a field in an after_save logic hook?

Hello all,

I have created a custom field to hold an autoincrement value for each record in the module. This works ok.

I would like to use this number as a part of the name of the record. So, I have created a logic_hook based on the after_save event.

Unfortunately, it seems that $bean data is not available when after_save-ing.

It I test the logic_hook in before_save event, the code works ok and Name field is properly updated. However I can't use the code in before_save because it only would work when editing records not when creating them, because the logic hook would be fired before setting the autoincrement value.

Any ideas?

Here is my code, so simple. I'm on SugarCRM CE 6.5.11.
class my_class{
  public function my_function(&$bean, $event, $arguments){
    $bean->name = $bean->my_autoincrement_field_c;
  }
}
Parents
  • In a case like this I'd go straight to the database and do an update query. Either that or in your after_save call do a $bean->save(). There is some built-in protection against nested logic hooks, but it would be wise to also set a static var to ensure that your hook only runs once if you call ->save() again.
  • Jaume,

    To add to eggsurplus's notes, in your after_save hook, you just need to :

    $bean->db->query(" UPDATE {$bean->table_name} SET name='{$bean->my_autoincrement_field_c}' WHERE id='{$bean->id}' ");

    This will update the name field in the bean's table with the value from the 'my_autoincrement_field_c'.

    But be careful whether you need the name to be set on EVERY save of the record as doing this without any checks/conditions will overwrite the "name" field if any other logic or users have updated it to something else.

    ie you might want to only update the name once and not everytime and/or based on certain checks/conditions.

    eg set it to the my_autoincrement_field_c value only if the name is empty

    class my_class{
      public function my_function(&$bean, $event, $arguments){        if(empty($bean->name)){
           $bean->db->query(" UPDATE {$bean->table_name} SET name='{$bean->my_autoincrement_field_c}' WHERE id='{$bean->id}' ");      }
      }
    }
    Cheers
    Kedar.
  • Why do an after save? That will change the name and save again which will call the after save again...
    Why not do a before_save instead? You should be able to just set the $bean->name as you have above:
    $bean->name = $bean->my_autoincrement_field_c;

    My general approach is that if you are changing fields of the bean you are saving then use a before_save, if you are changing another bean then you can use an after_save.

    HTH
    FrancescaS
  • I think of this differently - why don't you make the name field the auto increment field instead?
    If this is for display only, then wouldn't it be easier to change the display using customCode?
Reply Children
  • rlai, thank you very much for your answer. I am not sure that you can change the data type of a field after its creation. On the other hand, I don't think that you can make a text field to be autoincremented by the database, I believe it might be an int type field to be autoincremented. In my case, it is possible that the name field would contain something else concatenated with that code, so I would prefer to keep a separated code field.
  • Hello,

    First of all I would like to thank everybody that has answered my question. You are so kind and have helped me to find the answer.

    Just in case it might be of others interest, here is the solution:

    1) The initial idea is what eggsurplus and SugarNinja firstly suggested: update the record in the database.

    2) The code suggested by SugarNinja was ok but had a little problem. The autonumeric value generated in the database when the record is saved for the first time is not being updated in the bean that is managed in the logic hook. So, the autonumeric field had a NULL value, which obviusly was not useful for our purposes.

    3) So what I have finally done is update the bean with the data in the database by just retrieving the record using this line of code:
    $bean->retrieve($bean->id);
    And that's all!

    So, here is my full logic hook code:

    <?php
        //prevents directly accessing this file from a web browser
        if(!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');

        class my_class {

            public function my_function(&$bean, $event, $arguments) {

                    if(empty($bean->name)) {

                        $bean->retrieve($bean->id);

                        $bean->db->query("UPDATE {$bean->table_name} SET name='{$bean->my_autoincrement_field_c}' WHERE id='{$bean->id}'");

                    }               

            }

        }

    Hope it helps!
  • Let me repeat the code. I will try to put it in a single box, now. Don't know why it is so difficult to paste code in this editor and get to format it properly without further edition. Furthermore now I can't edit the previous comment, so I have to creat this one.
    <?php
        //prevents directly accessing this file from a web browser
        if(!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');

        class my_class {
            public function my_function(&$bean, $event, $arguments) {
                    if(empty($bean->name)) {
                        $bean->retrieve($bean->id);
                        $bean->db->query("UPDATE {$bean->table_name} SET name='{$bean->my_autoincrement_field_c}' WHERE id='{$bean->id}'");
                    }               
            }
        }

  • Is there a way to check a record for uniqueness while entering on the go?

  • Hi All

    In After Save You can update Your Module Data Using Bean

    Ex-

    $NewCaseBean = BeanFactory::newBean("ModuleName");
    $NewCaseBean->name=$bean->name."Nika";
    $NewCaseBean->save();

    Thanks