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.
  • Auto Increment fields are set at the database layer once DB Manager has control over its iterator, so such a field only will be populated after the very record is saved into db. Having said that, even if you were able to select an auto increment field inside a sugarLogic formula, its value would be empty once SugarCRM has no control over its very value.

    So the unique suitable approach is to implement a LogicHook after_save.

    Cheers

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Hello

    Just to add in what André just shared, you can find a guide on how to implement this on the link below: 

    support.sugarcrm.com/.../

  • Hi,

    I understand why it is asked for formula - it may look confusing that such a simple task may require the development of a CRM with coding.

    The truth is that learning Developer Guides and diving into PHP are not mandatory to get this simple configuring to work.



    I employed Logic Builder configuring tool to setup the logic hook colleagues have mentioned here and get this Sugar behavior - it took just a few minutes.

    Here is a flowchart that implements the task, you may read it by following the white line (just zoom in, if necessary)



    Since I have access to the Logic Builder tool (https://logicbuilder.integroscrm.com), you may just let me know the exact names of the fields (that lasts with _c) and the name of the module fields belong to (like this https://<url>/#i_CustomA ),  and I will get back with a ready-to-install zip, which Logic Builder generates in a click.


    Best Regards,
    Dmytro Chupylka

    integroscrm.com
    We make work in Sugar CRM system faster, more convenient and efficient

  • 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

  • Not sure which version of Sugar you have, but just in case wanted to make aware Sugar added AutoIncrement field type recently:

    .

    CRM Business Consultant

  • it's a standard field type you can add in Studio since v10.2
    support.sugarcrm.com/.../

    .

    CRM Business Consultant