I want to change custom field length through PHP Script. How to write a PHP Script for that and Where I have to keep that Script in Sugar 8.0 localhost?
I want to change custom field length through PHP Script. How to write a PHP Script for that and Where I have to keep that Script in Sugar 8.0 localhost?
Good morning Nagamani D,
I think you would probably have to define your own Endpoint ( https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.0/Integration/Web_Services/REST_API/Extending_Endpoints/#Defining_New_Endpoints ) , and change the size in at least three places:
- the fields_meta_data table
- the _cstm table where your field actually is
- the vardefs for that module
You will need to refresh your cache after the changes are made. You could maybe get away with just the database changes if you can then execute a QRR to update the vardefs and cache. But I may very well have forgotten some steps, so take this with a grain of salt.
It really should be a one time thing to set the field length, and not something you need to do, or should do repeatedly, so I am curious, may I ask what your business case is for wanting to change field length through PHP instead of Studio?
FrancescaS
We want to upgrade our project from Sugar 8.0.3 to 9.0. While doing upgradation, we are facing problems with the field length.
Thank you Francesca Shiekh,
Can you please provide an exact solution?
I have written one script using PHP. If I execute that script, length value is only updating in the database. But not updating in the studio
<?php
if(!defined('sugarEntry'))
define('sugarEntry', true);
require_once ('include/entryPoint.php');
require_once('data/BeanFactory.php');
require_once('include/database/DBManager.php');
require_once('include/utils.php');
require_once('include/SugarQuery/SugarQuery.php');
$GLOBALS['db'];
$field_name = 'testing_c';
$new_length = 20;
$query = "ALTER TABLE accounts_cstm modify $field_name VARCHAR($new_length)";
$result =$db->query($query);
?>
Hi Nagamani D
I would suggest you to do this change using vardef extension instead of writing plain SQL query.
For your requirement, go to custom/Extension/modules/<module_name>/Ext/Vardefs directory and check if file sugarfield_<field_name>.php exists, if not exists create a new file and append the following content to it.
<?php
$dictionary['<module singular>']['fields']['<field_name_c>']['len'] = <your_value>; //<module_singular> means for Accounts, it should be Account
Save the file, go to Admin -> Repair and perform Quick Repair and Rebuild.
After the repair is completed, you will notice a section at the bottom stating that there are differences between the database and vardefs. Execute the script to propagate the change to your database.
More information on vardefs here - https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_8.3/Data_Framework/Vardefs/Manually_Cre…
Let us know if this helps.
Regards.
Then I would recommend you do the field size changes using studio before the upgrade. This will ensure that your changes are applied to both the database and the vardefs.
The solution hats suggested here would work for you, depending on the field type, but you still have to edit the vardefs for each field you want to change and you still have to run the QRR. So why not just do it from Studio?
FrancescaS
Definitely doing through the field definition would be best. I just was running into this same issue and you also need the fields_meta_table updated for custom fields otherwise your table column definitions will revert back.
I have another Doubt that is, How to make shipping address field non-editable using Studio?
Hi Nagamani D,
You can easily do it from record.php file by adding 'readonly' attribute to the field array.
array(
'name' => 'shipping_address',
'type' => 'fieldset',
'css_class' => 'address',
'label' => 'LBL_SHIPPING_ADDRESS',
'readonly' => true, //make shipping_address readonly
.......
At this point, we do not have any straight forward way to make this field read-only from the Studio.
Regards.
Thank you hats