AnsweredAssumed Answered

how can i check duplicate phone_number using create-actions.js ?

Question asked by Bhavin Patel on May 4, 2016
Latest reply on Aug 30, 2016 by Offshore Evolution

Hello

 

i tried to check duplicate phone_number from create time.

if phone_number is duplicate that time it is shows the error massage and disable save button.

i use this code for finding duplicate mobile record

custom/modules/Contacts/clients/base/api/duplicateMobile.php

 

class duplicateMobile extends SugarApi
{
  public function registerApiRest()
  {
   return array(
    'duplicateMobile' => array(
    'reqType' => 'POST',
    'path' => array('Contacts','duplicateMobile'),
    'pathVars' => array(),
    'method' => 'duplicateMobile',
    'shortHelp' => 'Custom Api to check for duplicate Mobile Number',
    'longHelp' => '',
    ),
   );
  }
public function duplicateMobile($api,$args)
  {

    global $sugar_config, $db;
   $Mobile = $args['Mobile_number'];
   $recordId    = $args['id'];
   $duplicateCheckQuery = $db->query("select count(id) as num,phone_mobile from contacts where phone_mobile='".$Mobile."' and deleted=0");
   $duplicateCheck = $db->fetchByAssoc($duplicateCheckQuery);
       if($duplicateCheck['num'] > 1)
       {
        $recordId = $duplicateCheck['phone_mobile'];
        return "true";  
       }
       else
       {
        return "false";
       }
 }
}

 

custom/modules/Contacts/clients/base/views/create-actions/create-actions.js

({  
extendsFrom: 'CreateActionsView',
initialize: function (options) {  
this._super('initialize', [options]);
this.model.on('change:phone_mobile',this.duplicateCheck,this);
self = this;
},
duplicateCheck:function(){
  
   if(!_.isUndefined(this.model)){
    if(this.model.get('phone_mobile')){
    
     var self=this;
     var result;
     var savebtn;
     saveButton=this.getField('save_button');    
     dataArray = {Mobile_number : this.model.get('phone_mobile'), id: this.model.get('id')}
     app.api.call('create',app.api.buildURL('Contacts/duplicateMobile'),dataArray,{
      success:function(response){
       result=response;
       // display the error if id already exists.
       if(result == 'true'){
        app.alert.show('Validate', {
         level  : 'error',
         messages : 'Mobile Number already exists',
         autoClose : true,
        });
        self.model.set('phone_mobile','');
        saveButton.setDisabled(true);
       } 
      },
     });
    }
   }
  },
})  

 

Any one have idea regarding same please share.

Outcomes