Help with Duplicate IBAN Detection for Array Field in EspoCRM

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KingdomCoder
    Junior Member
    • Sep 2024
    • 2

    Help with Duplicate IBAN Detection for Array Field in EspoCRM

    For each customer, I need to store multiple IBANs, so I’ve set up the IBAN field as an array. Now, I want to implement a duplicate check that warns me if I enter an IBAN that already exists for another customer.

    However, I’m struggling to achieve this using EspoCRM scripting. Since the IBAN field is structured as an array, I’m unsure how to check for duplicates across all customers.

    Has anyone here dealt with similar problem or has an idea of how I could implement this? Is there a way to validate an array and compare it against the other iban arrays?

    Any suggestions or insights would be greatly appreciated!

    Thanks in advance!
  • victor
    Active Community Member
    • Aug 2022
    • 729

    #2
    In Administration > Entity Manager > СEntity > Formula > API Before Save Script insert the following code:

    Code:
    $i = 0;
    
    $ibanCount = array\length(iBAN);
    
    while ($i < $ibanCount) {
        
        $ibanItem = array\at(iBAN, $i);
        
        $id = record\findOne('CEntity', 'createdAt', 'desc', 'iBAN*', string\concatenate('%"', $ibanItem, '"%'));
        
        if ($id) {
            recordService\throwConflict(string\concatenate('Record with ', $ibanItem, ' IBAN exists.'));
        }
    
        $i = $i + 1;
    }​
    In the formula, replace only CEntity with the name of your entity.

    Comment

    • KingdomCoder
      Junior Member
      • Sep 2024
      • 2

      #3
      Thank you! That saved me a lot of time.
      I made a few changes, especially using throwDuplicateConflict . I thought that was more the case. Works like a charm.

      Code:
      $i = 0;
      
      $ibanCount = array\length(iBAN);
      
      while ($i < $ibanCount) {
          
          $ibanItem = array\at(iBAN, $i);
          
          $idsOriginal = record\findMany('Contact',20, 'createdAt', 'desc', 'iBAN*', string\concatenate('%"', $ibanItem, '"%'));
          
          $n=0;
          while ($n < array\length($idsOriginal)){
              $id=array\at($idsOriginal, $n);
              if ($id) {
                  if ($id!=id){
                       recordService\throwDuplicateConflict($id);
                  }
            
              }
              $n = $n +1;
          }
      
          $i = $i + 1;
      }
      ​

      Comment

      Working...