Question: How To Create A Sugar Logic Formula For AutoIncrement Field

Hello, is there a way to be able to create a Sugar Logic Formula that includes the AutoIncrement field?

Sample Setup:

  • Field #1:
    • Text Field
    • Value: Name
  • Field #2:
    • AutoIncrement Field
    • Value: 150
  • Field #3:
    • Text Field
    • Sugar Logic Formula Enabled
      • Formula: Field #1-Field #2
    • Value: Name-150

Problem:

  • It seems the Auto-Increment field isn't selectable on the Sugar Logic formula window. Are there any alternative formula I can use?

    Kindly advise, thank you everyone.
Parents
  • As André said a logic hook is the best solution.

    It is actually a very simple logic hook (this code is not tested or verified, so take it with a pinch of salt, but it should get you started on the right track):

    in the module's directory create a logic hook file, let's call it NameYourClass_Logic.php

    class NameYourClass_Logic {
    
      //before_save
      //this before_save function allows us to see if the value of the Name in Field1 has changed, in which case we want to update Field3
      //the fetched_row array contains the values of the fields before the change is made, IF this is not a new record.
      //if this is a new record then $bean->fetched_row is empty and this will not run, we take care of the new records in the after save
      //where we check if field3 is empty and set it with the newly assigned name in field1 incremental value from field2 
    
      function updateField3 ($bean, $event, $arguments){
         if (isset($bean->fetched_row) && $bean->fetched_row["field3"] != $bean->field3) {
          $bean->field3 = $bean->field1 . '- ' . $bean->field2;
          //do NOT save a bean in the before save, this code executes before the save so it just sets the field which will be saved
        }
      }
    
      //after save
    
      function setField3 ($bean, $event, $arguments) {
        //check if Field3 is empty, if it is already populated we don't need to do it again, this will also avoid infinite loops 
        //if you save a bean in an after save the save will trigger a before save and an after save again so you need to have a way to stop it
        //the if statement will stop this hook from executing if field3 is already populated
    
        if (empty($bean->field3) && !empty(field1) && !empty(field2) || ){
            $bean->field3 = $bean->field1 . '- ' . $bean->field2;
            $bean->save(); //here you have to save explicitly, this is an after-save hook
        }
      }

     

    in the module's directory, add or edit your logic_hooks.php file to list these two logic hooks:

    // Do not store anything in this file that is not part of the array or the hook version. 
    //This file will be automatically rebuilt in the future. 
    
    $hook_version = 1;
    $hook_array = Array();
    
    $hook_array['before_save'] = Array();
    $hook_array['before_save'][] = Array(1,'updateField3','custom/modules/YourModule/NameYourClass_Logic.php','NameYourClass_Logic','updateField3',);
    
    $hook_array['after_save'] = Array();
    $hook_array['after_save'][] = Array(1,'setField3','custom/modules/YourModule/NameYourClass_Logic.php','NameYourClass_Logic','setField3',);
    

    I hope this helps you get going and more interested in building your own logic hooks, if you are not already doing so :)

    FrancescaS

Reply
  • As André said a logic hook is the best solution.

    It is actually a very simple logic hook (this code is not tested or verified, so take it with a pinch of salt, but it should get you started on the right track):

    in the module's directory create a logic hook file, let's call it NameYourClass_Logic.php

    class NameYourClass_Logic {
    
      //before_save
      //this before_save function allows us to see if the value of the Name in Field1 has changed, in which case we want to update Field3
      //the fetched_row array contains the values of the fields before the change is made, IF this is not a new record.
      //if this is a new record then $bean->fetched_row is empty and this will not run, we take care of the new records in the after save
      //where we check if field3 is empty and set it with the newly assigned name in field1 incremental value from field2 
    
      function updateField3 ($bean, $event, $arguments){
         if (isset($bean->fetched_row) && $bean->fetched_row["field3"] != $bean->field3) {
          $bean->field3 = $bean->field1 . '- ' . $bean->field2;
          //do NOT save a bean in the before save, this code executes before the save so it just sets the field which will be saved
        }
      }
    
      //after save
    
      function setField3 ($bean, $event, $arguments) {
        //check if Field3 is empty, if it is already populated we don't need to do it again, this will also avoid infinite loops 
        //if you save a bean in an after save the save will trigger a before save and an after save again so you need to have a way to stop it
        //the if statement will stop this hook from executing if field3 is already populated
    
        if (empty($bean->field3) && !empty(field1) && !empty(field2) || ){
            $bean->field3 = $bean->field1 . '- ' . $bean->field2;
            $bean->save(); //here you have to save explicitly, this is an after-save hook
        }
      }

     

    in the module's directory, add or edit your logic_hooks.php file to list these two logic hooks:

    // Do not store anything in this file that is not part of the array or the hook version. 
    //This file will be automatically rebuilt in the future. 
    
    $hook_version = 1;
    $hook_array = Array();
    
    $hook_array['before_save'] = Array();
    $hook_array['before_save'][] = Array(1,'updateField3','custom/modules/YourModule/NameYourClass_Logic.php','NameYourClass_Logic','updateField3',);
    
    $hook_array['after_save'] = Array();
    $hook_array['after_save'][] = Array(1,'setField3','custom/modules/YourModule/NameYourClass_Logic.php','NameYourClass_Logic','setField3',);
    

    I hope this helps you get going and more interested in building your own logic hooks, if you are not already doing so :)

    FrancescaS

Children
No Data