Check duplication based on field condition in sugarcrm 7

Am checking duplicate check in Accounts module based on some filed condition.

In Account i have a dropdown field called Account Type with options as
1. Organisation

2. Individual

if i select Account Type == Organisation then i have to check duplicates for some specific fields field1,field2,field3,field4.

and

if i select Account Type == Individual then i have to check duplicates for fields field1,field2,field5,field6.

How this can be achieve in sugarcrm 7.7 version.

I found one article for Duplication check but i want conditional based duplication checkup

Can any one please help me out from this

Thanks in advance

Regards

Sam

  • The filter based check is what we support out of the box.

    But that article you referred to also describes how you can implement your own custom Duplicate Check strategy class which would give you more control over how duplicate check is run.

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.7/Architecture/Duplicate_Check/#Custom…

    App Ecosystem @ SugarCRM

  • Hi Sam,

    I am not sure but this may work:

    $dictionary['Account']['duplicate_check']['FilterDuplicateCheck'] = array(
        'filter_template' => array(
            array(
                '$or' => array(
                    array(
                        '$and' => array(
                            array('account_type' => array('$equals' => 'organisation')),
                            array('field1' => array('$equals' => '$field1')),
                            array('field2' => array('$equals' => '$field2')),
                            array('field3' => array('$equals' => '$field3')),
                            array('field4' => array('$equals' => '$field4')),
                        )
                    ),
                    array(
                        '$and' => array(
                            array('account_type' => array('$equals' => 'individual')),
                            array('field1' => array('$equals' => '$field1')),
                            array('field2' => array('$equals' => '$field2')),
                            array('field5' => array('$equals' => '$field5')),
                            array('field6' => array('$equals' => '$field6')),
                        )
                    )
                )
            ),
        )
    );
    
  • Hi Sam Roy

    You can try following code, which worked for me:

    ./custom/Extension/modules/Accounts/Ext/Vardefs/<file>.php

    $dictionary['Account']['duplicate_check']['FilterDuplicateCheck'] = array(
        'filter_template' => array(
            array(
      '$or' => array(
      array(
      '$and' => array(
      array('account_type' => array('$equals' => 'Organisation')),
      array(
      '$or' => array(
      array('field1' => array('$equals' => '$field1')),  
      array('field2' => array('$equals' => '$field2')),  
      array('field3' => array('$equals' => '$field3')),  
      array('field4' => array('$equals' => '$field4'))
      )
      ),
      )
      ),
      array(
      '$and' => array(
      array('account_type' => array('$equals' => 'Individual')),
      array(
      '$or' => array(
      array('field1' => array('$equals' => '$field1')),  
      array('field2' => array('$equals' => '$field2')),  
      array('field5' => array('$equals' => '$field5')),  
      array('field6' => array('$equals' => '$field6'))
      )
      ),
      )
      ),
      )
            ),
        ),    
    );
    

    Or if you want to check for more condition base then go for below code:

    ./custom/Extension/modules/Accounts/Ext/Vardefs/<file>.php

    <?php
    $dictionary['Account']['duplicate_check'] = array(
      'enabled' => true,
      'OneFieldDuplicateCheck' => array(
                'field' => 'name'
            )
    );
    

    Replace OneFieldDuplicateCheck with following:

    'MultipleFieldDuplicateCheck' => array(            
                'field' => 'account_type',
                'field1' => 'xxxxx',
                'field2' => 'xxxxx',
                'field3' => 'xxxxx',
                'field4' => 'xxxxx',
                'field5' => 'xxxxx'
            )
    

    Now ./custom/data/duplicatecheck/MultipleFieldDuplicateCheck.php

    <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    
    
    class MultipleFieldDuplicateCheck extends DuplicateCheckStrategy
    {
    
    
        protected $field;
    
    
        public function setMetadata($metadata)
        {
            if (isset($metadata['field'])) {            
                $this->field1 = $metadata['field1'];
                $this->field2 = $metadata['field2'];
                $this->field3 = $metadata['field3'];
                $this->field4 = $metadata['field4'];
                $this->field5 = $metadata['field5'];
            }
        }
    
    
        public function findDuplicates()
        {
            if($this->bean->{$this->field} == 'Individual'){
      $Query = new SugarQuery();
      $Query->from($this->bean); $Query->where()->queryOr()->ends($this->field1,$this->bean->{$this->field1})->equals($this->field2,$this->bean->{$this->field2})->equals($this->field5,$this->bean->{$this->field5})->equals($this->field6,$this->bean->{$this->field6});
      //Filter out the same Bean during Edits
      if (!empty($this->bean->id)) { 
      $Query->where()->notEquals('id',$this->bean->id);
      }
      $Query->limit(10);
      }else if($this->bean->{$this->field} == 'Organisation'){
      $Query = new SugarQuery();
      $Query->from($this->bean); $Query->where()->queryOr()->ends($this->field1,$this->bean->{$this->field1})->equals($this->field2,$this->bean->{$this->field2})->equals($this->field3,$this->bean->{$this->field3})->equals($this->field4,$this->bean->{$this->field4});
      //Filter out the same Bean during Edits
      if (!empty($this->bean->id)) { 
      $Query->where()->notEquals('id',$this->bean->id);
      }
      $Query->limit(10);
    
      }  
      $results = $Query->execute();        
            return array(
                'records' => $results
            );
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    

    Hope this Helps

    Best Regards

    S Ramana Raju

  • Hi David López

    Thanks for the reply, i will try and get back to you.

    Regards

    Sam

  • Hello Ramana Raju Santhana

    I was checking for multiple option duplicate check and your post is very helpful.

    Thanks for sharing your experience.

    Regards

    Syed Sharif

  • Hi Ramana Raju Santhana

    You are truly truly Genius Ramana.

    I had a requirement with this information i have completed.

    Before finding this post i have really searched and googled for 2-3 Days then i thought to post in community.

    May be by luck or dont know, i get into this post and found solution for my problem.

    Its very clear and truly from my bottom of my heart i appreciate you for sharing your expertise.

    Also i congrats you to get into Top 5 list that too in shot period of time(Almost in 50 Days ).

    Cheers for that and hope to reach in Top soon.

    Last but not the least very thanks for Sam Roy for asking this question.

    Regards

    John