SugarCRM restriction on custom field - sugarcrm

i'm totally new to SugarCRM module development, however i have very good knowledge of PHP, ajax and database programming.
Here is my task:I have been asked to create a restriction on a custom field from the clients module.
There is a custom field called identification number, what i need to do is avoid a new client to be saved into the database based on that field, in other words the client has to be unique. It has to display a pop up window saying "that client already exists"

Copy editviewdefs.php of Accounts to the custom folder and change the custom field definition for id_number to this
array(
'name' => 'id_number',
'displayParams' =>
array (
'field' =>
array (
'onChange' => 'check_is_duplicate(this);',
),
),
),
Create a javascript function check_is_duplicate
function check_is_duplicate(obj) {
// Call a script via Ajax. Pass values for id and id_number with the request.
if (o.responseText > 0) {
alert('duplicate');
document.getElementById('SAVE').disabled = true;
} else {
document.getElementById('SAVE').disabled = false;
}
}
You would have to create the script which would be called via ajax request.
In that script you would have to run a query like -
SELECT COUNT(*) AS count FROM accounts
WHERE deleted = 0 AND id != {$record} AND id_number = {$id_number}
Execute the query and return the count.
Afterwards on saving check for duplicates from server side using the beforeSave logic hook.

You can create a custom duplicate check by the following code:
$dictionary['Account']['duplicate_check']['FilterDuplicateCheck']['filter_template'] = array(
array(
'$and' => array(
array('identification_number' => array('$equals' => '$identification_number')),
),
),
);
Add this code in the file: /custom/Extension/modules/Accounts/Ext/Vardefs/duplicate_check.php.
Then, perform a quick repair/rebuild.
How it works:
When you enter the identification number and press save, SugarCRM will perform a duplicate check. If duplicates are found, SugarCRM will list the duplicates and you can choose to use the duplicate or ignore.

Related

Move existing pages into newly created pages with TYPO3 DataHandler

I am using the DataHandler to create and move pages like in this snippet. While new pages are created fine, existing subpages are not moved into their newly created parents.
This creates the new page but does not move the existing page
$data = [
'pages' => [
'NEW_IT' => [
'pid' => 1,
'hidden' => false,
'title' => 'IT',
],
591 => [
// this is not set
'pid' => 'NEW_IT',
// but this is set
'title' => 'I am a child of IT',
],
]
];
I tried ['pages'][591]['move'] = 'NEW_IT' but also to no avail.
I also tried '591' instead of 591, dataHandler->reverseOrder = true and dataHandler->copyTree = true.
dataHandler->errorLog is empty.
In contrary, this works (new page into new page)
$data = [
'pages' => [
'NEW_IT' => [
'pid' => 1,
'hidden' => false,
'title' => 'IT',
],
'NEW_IT_SUB' => [
'pid' => 'NEW_IT',
],
]
];
Also I wonder which IDs (NEW<any string> vs. NEW<base64> etc.) are acceptable as I did not find anything in the documentation and the examples use different styles. The "must be unique" is obvious. But I don't get why some people generate UUIDs there.
References
https://docs.typo3.org/m/typo3/reference-coreapi/9.5/en-us/ApiOverview/Typo3CoreEngine/Database/Index.html
EDIT: I opened a forge ticket: https://forge.typo3.org/issues/90939
I have checked the code / DataHandler in v8/v9 and master. The corresponding logic in the DataHandler has not changed for that.
I have created a test case and xdebugged through it, also i was pretty sure after looking into the code, what happens. But have done this to validate that "it is working the way i think the code tells" - although it is not the way you expected. And I'm too. Remembering that I had such an issue last year through a migration script, but did not digged deeper into it (because of time), i changed it and made this as loops.
Creating page => retrieving the replaced id => using it for the other data commands.
The datahandler loops through each record for a table in the provided dataset array.
foreach ($this->datamap[$table] as $id => $incomingFieldArray) { ... }
First it prepares some stuff, and calling registered processDatamap_preProcessFieldArray hooks, it checks the given $id
// Is it a new record? (Then Id is a string)
if (!MathUtility::canBeInterpretedAsInteger($id)) { ... } else { ... }
If the id is not an integer or an integer string, it executed the true-branch, otherwise the false-branch. So long, so good.
The TRUE-Branch handles creating a new record (page, tt_content, ...), adding the replacement for the provided NEWxxx as substituion and so on.
It also checks, if a 'pid' field is in the record, and if it contains with NEW. If it is so, it replaces NEWxxx with the uid of the previously created record.
On the otherhand, if $id is not a integer or a integer string, it assumes it is an existing record. (FALSE-Branch). On this branch there is no checking for 'pid', if it contains NEW and looking up for replacement.
But .. why is there no error ? If it did not replace it, it should crash or someting like that ? - Yes, that is what we would assume - at least for that.
'pid' is always ignored in appling to the record ( see method fillInFieldArray() )
switch ($field) {
case 'uid':
case 'pid':
// Nothing happens, already set
break;
...
}
and so .. as long as you do not provide further fields/values in the second page array ( that with the integer id and the pid with NEW from the initial), it has nothing to do. Nothing to do is not an error - and so, it do nothing and even do not notify anything about it.
Is it a bug ? Don't know, personally I would say yes. But maybe this should be created as an issue, and eventually discussed with the core developer/other contributers.
Maybe there were/are reasons WHY it only do this one run.
Either the documentation should be changed to make clear, that 'pid' replacement only works, id $id => [] is also a new / playholder id .. or you have to do it in 2 rounds.
Or .. put as as feature/bug to the issue tracker, and let's discuss it. And maybe giv it a try to implement it in the FALSE/existing record branch too. But let's heare some other opionons about it.
If others thing this should be also be done for the existing records in the same round, I would take the time tommorrow/at the weekend to provide an patch for it / the issue. (Would be fun to fight with the datahandler ).

How to unset field from Document

I have one document in which I want to remove one field. I got answer from Here. But, It is for core PHP code and I want in CakePHP how to unset field from document.
I tried following code in CakePHP.
$this->MyModal->id = $id;
$this->MyModal->updateAll(
array('myField' => array('$exists' => true)),
array('unset' => array('myField' => true))
);
Edit
I also tried
$this->MyModal->updateAll(
array('$unset' => array('myField' => 1))
);
But it does not work.How can unset field?
It looks like you're using wrong the update all method. First you must provide an array with the new values you want for your fields and then you should pass an array with the search criteria. So in order for it to work you could write something like
$conditions=array('myField'=>array('$exists'=>true));
$this->MyModal->updateAll(
array('$unset'=>array('myfield'=>1)),
$conditions//array of search conditions
)

How to add custom subpanel on detail view page without relationship

How to add custom subpanel on detail view page without relationship
Eg.: I want to add Accounts subpanel on Leads Detail View Page.
This might give you an edge towards the correct answer but you should be aware that it is fairly involved.
You can create a custom dashlet by adding a file to the director custom/Extension/modules/Leads/Ext/Layout/name_this_file_anything.php
The content of the file should be something add a new element to the array $layout_defs['Leads']['subpanel_setup']. You can probably find what to add from the layout defs files in the existing modules directory.
If you need to create a custom subpanel that is quite a bit more work and involves creating a function called "getSubpanelQueryParts($params)" which queries the required records and returns an array of query parts and adds an element to the subpanel_setup/custom_dashlet array which reads something like this:
'collection_list' => array(
'calls_opportunities' => array(
'subpanel_name' => 'ForAccounts',
'module' => 'Calls',
'get_subpanel_data' => 'function:getSubpanelQueryParts', // here custom method defined
'generate_select' => true, // to build custom SQL query
'function_parameters' => array(
'import_function_file' => 'custom/application/Ext/Utils/custom_calls_opportunities.php',
'return_as_array' => 'true'
), // to get data for subpanel collection item
),
),

How to perform add action in a form only one time, after update the record everytime

I have a form element. When I click the submit button it will add the data to the table.
that is working fine. But my need is to add data to the table only the first time, after each submission data should updated.how we can do this?
function add(){
if(!empty($this->data)){
$this->loadModel('Defineroute');
$this->Defineroute->create();
$this->Defineroute->save($route_data); //route_data is data from the form
}
}
I have one more doubts, I have a view.ctp in the controller I have a view() function for that. But in the view.ctp I use one form element.
How we can write function for that element in the controller? When I try to write function, it shows the error missing view..
CakePhp determines if it should update an existing record or insert a new record based on the presence of the primary key ('id') inside the data you're saving;
This will insert a new record:
$route_data = array(
'Defineroute' => array(
'name' => 'I am a new record'
)
);
$this->Defineroute->save($route_data);
And this will update an existing record;
$route_data = array(
'Defineroute' => array(
'id' => 123,
'name' => 'I will update record with ID 123'
)
);
$this->Defineroute->save($route_data);
To accommodate this in a single form, create the form inside your view/element and only add an input for the id field if you're editing an existing record
Also see Saving your data

Symfony2 entityaudit: Add field on revision table

I have 2 entities in my bundle that are audited via simplethings/entity-audit.
I would like to add a field to REVISIONS table call "reason". Every time a user updates or delete an entity, he/she needs to especicify an reason for doing that (why updating/deleting) via form, and this reason should be associated to the entity revision.
How would you guys do it? I dont have much experience in OOP.
Thank you very much in advance.
for adding field you need to add field in your database like 'ip' next you change your bundle in file "logRevisionsListener.php"
private function getRevisionId()
{
if ($this->revisionId === null) {
$this->conn->insert($this->config->getRevisionTableName(), array(
'timestamp' => date_create('now'),
'username' => $this->config->getCurrentUsername(),
'ip' => $this->config->getCurrentUsername(),(not correct just for test it give me the user name)
), array(
Type::DATETIME,
Type::STRING,
Type::STRING
));
.
.
}
I added here the ip field and change your Revision.php file by adding your field with the getter method