Custom validation for unique custom field

I’m trying to do a custom validation.
I have a module in which there is a field that must be unique, so suitecrm must not allow you to create two records with that same field.
HELP

Thank you

I am not sure, but maybe you can get it done with this technique

https://gunnicom.wordpress.com/2015/09/21/suitecrm-sugarcrm-6-5-add-custom-javascript-field-validation/

or maybe with this other way:

https://suitecrm.com/suitecrm/forum/developer-help/6921-viewedit-validation

Good luck!

Thanks.
The second solution adds the required field requirement. The first solution I tested and it works, but I would need to check if the value of the field is already present in the database, so instead of checking if the field is equal to certain values, I should compare it with the values already present and if I find an equal one, bring up a message that does not allow saving. Instead of checking:

if($("#" + nameIndex).val()!=999){add_error_style(formname,nameIndex,“Only 999 is allowed!”); return false;}; return true;

I should query the database.
Thank

does anyone have solutions? Thank you

Sorry, I don’t understand what your problem is exactly.

You don’t know how to query the database? Have you checked the developer guide on how to use Beans?

I would like the software to check the database before saving the record and if it finds another value of the field concerned (the field that must be unique, without duplicates) equal to the one entered, it will not allow me to save the changes (or new record) and write me that the field is already present.

a behavior similar to that described on this page:

https://stackoverflow.com/questions/23340660/prevent-duplicate-value-using-ajax-in-sugar-crm

I tried the procedure described on this page but no it works

I would go for a before_save logic hook and then try to get a bean with get_list method. You don’t need to query the database.

You can find documentation and some examples here:

https://docs.suitecrm.com/developer/working-with-beans/

See below what is in that page:

get_list
The get_list method allows getting a list of matching beans and allows paginating the results.

Example 3.5: get_list method signature
get_list(
$order_by = “”,
$where = “”,
$row_offset = 0,
$limit=-1,
$max=-1,
$show_deleted = 0)

Then use the row_count property of the return array. If row_count > 0 then it is a duplicated field.

I think it solves your problem.

Rgds

Marcio

I could insert the get_list method inside the function:

callback’ => ‘function(formname,nameIndex){if($("#" + nameIndex).val()!=999){add_error_style(formname,nameIndex,“Only 999 is allowed!”); return false;}; return true;}’,

or should I insert get_list necessarily inside a before_save hook?

Good idea! Better than the hook. It should work. Try and tell us!

I tried but it doesn’t work. maybe I’m wrong in the code.
Could you write the code to see what is wrong? thank you

could I write something like that inside the function declared in callback?

require_once(‘include/SugarQuery/SugarQuery.php’);
$sugarQuery = new SugarQuery();

//fetch the bean of the module to query
$bean = BeanFactory::newBean(’’);

//create first query
$sql = new SugarQuery();
$sql->select(‘id’);
$sql->from($bean);
$sql->Where()->equals(’’, ‘’);

$result = $sql->execute();

$count = count($result);

if ($count == 0) {
//no results were found
} elseif ($count == 1) {
//one result was found
$bean = BeanFactory::getBean(’’, $result[0][‘id’]);
} else {
//multiple results were found
}

I thought of writing this in vardefs:

<?php

$dictionary['AV4_RichiesteH']['fields']['quantita_c']['inline_edit']='';
$dictionary['AV4_RichiesteH']['fields']['quantita_c']['labelValue']='Quantita';
$dictionary['AV4_RichiesteH']['fields']['quantita_c']['type']='int';
$dictionary['AV4_RichiesteH']['fields']['quantita_c'] = array(
    'validation' => array(
         'type' => 'callback',
         'callback' => 'function(formname,nameIndex){
		                       var name = document.forms[formname][nameIndex].value;
                               $richiestebean = BeanFactory::getBean('AV4_RichiesteH');
                               $resultrichieste = $richiestebean->get_list("","quantita_c=name",0,-1,-1,0);
                                     if (!isset($resultrichieste)){
                                            return true;
                                     }else {
                                            add_error_style(formname,nameIndex,"Only 1 is allowed!");
                                            return false;
                                     };
                        }',

                    ),
);
?>

can it be okay?

Hi @Demis & @pgr

In continuation of above mail tread, We wanted to add another checkboxes, under form of Fields tab (under each module - screenshot below), The objective of insertion of the field is to ensure that there are no duplicate available in the system for a particular variable defined by the client.

Request to please help us where we should do customization so that same form is updated across all modules and changes are available.

I would love the addition of this “Check Duplicates” feature!!! I would be willing to financially support a small amount of development time if we could get it into the core. Thanks.

1 Like