Script to run before the view is created.

HI, thanks.
I have a special requirement.
We created a custom module from opportunities, named bookings and a MasterData Module to keep record for the booking id, this number must increment for any new booking record, which it does for now.
But what I need, is that this number appears when creating a new record.
I already tried on creating a script in js, on creating the view, (extendsFrom: 'CreateView') but it seems it runs globally every time a view is created, it's very sensitive a minimal error will block the whole instance. I feel sorry everytime we required sugar support to helps us initialize the instance again.
I was wondering if there's a better place to implement this script, a logic hook or something like that, 
Thanks again.
  • It is not quite clear. Can you kindly better describe the requirement?

    A mockup is welcome.

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • If I understand you correctly, you are looking for is an integer field that auto-increments for each new record.

    I have a custom module called wcont_Contracts which has a Contract Number field that is populated by the system as an auto increment number.

    The Contract Number is an Integer, Required, Read-Only field with auto_increment set to true.

    In the vardef of my field is as follows:

      'contract_number' =>
      array (
        'type'=>'int',
        'required' => true,
        'name' => 'contract_number',
        'vname' => 'LBL_CONTRACT_NUMBER',
        'readonly' => true,
        'len' => '11',
        'required' => true,
        'auto_increment' => true,
        'unified_search' => true,
        'full_text_search' =>
        array (
          'boost' => '3',
          'enabled' => true,
        ),
        'comment' => 'Visual unique identifier',
        'duplicate_merge' => 'disabled',
        'disable_num_format' => '1',
        'duplicate_on_record_copy' => 'no',
        'audited' => false,
        'massupdate' => false,
        'merge_filter' => 'disabled',
        'calculated' => false,
        'enable_range_search' => false,
        'min' => false,
        'max' => false,
      ),

    Then on the database I altered the field to be an auto-increment and start the numbers from 10000

    ALTER TABLE `wcont_contracts`
    CHANGE COLUMN `contract_number` `contract_number` INT(11) NOT NULL AUTO_INCREMENT, ADD INDEX `contract_number` (`contract_number`);
    alter table wcont_contracts AUTO_INCREMENT = 10000;

    You won't see the number in the record until after you Save, but it is created and entered for you.

    Hope this helps,
    FrancescaS

  • Thank you Andre, I'm sorry I didn't express myself quite enough.
    What I'm trying to do is. For example when a person let's say a receptionist, introduce and create new booking for a guest. I need display the next booking number, this is a autoincremental number, that I hold on the MasterData module, it increments, every time  a record is saved. (example 100,101,102). I have no problem with this.
    But what i need, is to display this number when the user create a new record. I already defined the variable and the field.
    I don't know how to auto fill this field, with this number. When the view is created for the new record.
    I tried to create a script for that, extending "CreateView", but it seems it affects all the views on the instance, even if the script is copied and defined only for this module. It seems to affects all the views on the instances, and it's very sensitive, any error, will complety block the whole instance.
    I tried also to implement a Logic Hook, but it seems there is not anything like "onCreate" event, only "beforeSave, afterSave".
    I hope you can understand  what I'm trying to do.
    I really apppreciate you help.
    Greetings.
  • HI, Francesca, I really appreciate you help.

    I have created a module, called MasterData, in this module I keep this autoincremental number. I managed this way, for the case, if the client needs or wants to change current number. The client can change it directly in this module.
    But mi problem is that I need to display this number when the user create a new record. I already defined the variable and the field.I don't know how to auto fill this field, with this number. When the view is created for the new record, so the person can see this number auto filled in the field.
    I increment this number with a Logic Hook, on "beforeSave" event.
    I tried to create a script for that, extending "CreateView", but it seems it affects all the views on the instance, even the script is copied and defined only for this module. And it's very sensitive any error, will block the whole instance.

    Thank you.

    Greetings.

  • It is not a good approach to previouly display the next booking number because, lets say, two receptionists create records at the very same time! It may through an exception on database once that field can not be duplicated. Actually we faced that twice on different customers years ago!

    So you really need to let SugarCRM to auto generate this field as soon as its record is saved into database.

    Regarding the issue on your instance, I'm afraid that your custom CreateView has not been properly implemented, which may been leading to a kind of looping.

    Francesca Shiekh's suggestion certainly will work, not exactly as you would like to have implemented, but this is the best approach you may find.

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Hi Andres, thank you.
    I'm aware of this situation, so I think my approach will be to increment this number when the user creates a new record, but only if the user cancels the record, this number will be lost.
    I talked to the client, but I don't know why, he really needs in this way.
    I will look forward on how to create this script.
    Greetings