duplicate check onsave

Hi guys,

I’m looking at adding the phone_mobile field to the duplicate check that happens when creating a lead, I thought I had narrowed it down to

/modules/Leads/LeadFormBase.php 

so created

/custom/modules/Leads/LeadFormBase.php

Edited this section to include the following.

Did a repair and rebuild and still not doing what I expected.

can anyone please help.

Thanks

public function getDuplicateQuery($focus, $prefix='')
{
	$query = "SELECT id, first_name, last_name, account_name, phone_mobile title FROM leads ";

    // Bug #46427 : Records from other Teams shown on Potential Duplicate Contacts screen during Lead Conversion
    // add team security

    $query .= " WHERE deleted != 1 AND (status <> 'Converted' OR status IS NULL) AND ";

    //Use the first and last name from the $_POST to filter.  If only last name supplied use that
	if(isset($_POST[$prefix.'first_name']) && strlen($_POST[$prefix.'first_name']) != 0 && isset($_POST[$prefix.'last_name']) && strlen($_POST[$prefix.'last_name']) != 0) {
		$query .= " (first_name='". $_POST[$prefix.'first_name'] . "' AND last_name = '". $_POST[$prefix.'last_name'] ."')";
	} else  {
		$query .= " last_name = '". $_POST[$prefix.'last_name'] ."'";
	} else {
		$query .= " phone_mobile = '". $_POST[$prefix.'phone_mobile'] ."'";
	}
    return $query;
}

You forgot to include another condition in “if” statement to check the phone number. Your query would be something like

if(isset($_POST[$prefix.'first_name']) && strlen($_POST[$prefix.'first_name']) != 0 && isset($_POST[$prefix.'last_name']) && strlen($_POST[$prefix.'last_name']) != 0 && isset($_POST[$prefix.'phone_work']) && strlen($_POST[$prefix.'phone_work']) != 0) {
		$query .= " AND (first_name='". $_POST[$prefix.'first_name'] . "' AND last_name = '". $_POST[$prefix.'last_name'] ."' AND phone_work = '". $_POST[$prefix.'phone_work'] ."')";
	} else {
		$query .= " last_name = '". $_POST[$prefix.'last_name'] ."'";
		
	}

This may not be perfect so make sure to test it out before deploying on production.

Thank you Arsalan,

I’m not having any luck, I have tried what you added and attempted it with the work phone number that you had in place and the system still carries on as though theres been no change, it’s not even broken the system, is it not possible to do this upgrade safe?

Ok so having searching around alot more, I found one post by a suitecrm admin which says its way to complicated to do. so I have decided to follow this post.

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

But I’m falling short at the first hurdle, where this guy is using book_name and book_id here I’m looking to use leads phone_mobile but i can’t wrap my head around it, been looking at it too long and need another pair of eyes. any help will be appreciated.

/* disclaimer: we are not gonna get all crazy with using PDO and parameterized queries at this point,
               but be aware that there is potential for sql injection here. The auth => true will help
               mitigate that somewhat, but you're never supposed to trust any input, blah blah blah. */

global $db; // load the global sugarcrm database object for your query

$book_name = urldecode($_REQUEST['book_name']); // we are gonna start with $_REQUEST to make this easier to test, but consider changing to $_POST when confirmed working as expected
$book_id   = urldecode($_REQUEST['book_id']);   // need to make sure this still works as expected when editing an existing record

// the $db->quote is an alias for mysql_real_escape_string() It still does not protect you completely from sql injection, but is better than not using it...
$sql = "SELECT id FROM book_module_table_name WHERE deleted = 0 AND name = '".$db->quote($book_name)."' AND id <> '".$db->quote($book_id)."'";

$res = $db->query($sql);

if ($db->getRowCount($res) > 0) {
    echo 'exists';
}
else {
    echo 'unique';
}

This is what worked for me using your first solution and also using Arsalan additional solution.

This function sends back the query to check for duplicates. Therefore it should send back the first and last names to the query if there is a potential phone_mobile
therefore the final code is as follows:

public function getDuplicateQuery($focus, $prefix=’’)
{
$query = "SELECT id, first_name, last_name, account_name, title, phone_mobile, phone_home FROM leads ";

// Bug #46427 : Records from other Teams shown on Potential Duplicate Contacts screen during Lead Conversion
// add team security

$query .= " WHERE deleted != 1 AND (status <> 'Converted' OR status IS NULL) AND ";

//Use the first and last name from the $_POST to filter.  If only first name supplied use that
if(isset($_POST[$prefix.'first_name']) && strlen($_POST[$prefix.'first_name']) != 0 && isset($_POST[$prefix.'last_name']) && strlen($_POST[$prefix.'last_name']) != 0) {
	$query .= " (first_name='". $_POST[$prefix.'first_name'] . "' AND last_name = '". $_POST[$prefix.'last_name'] ."')";
} 
else if(isset($_POST[$prefix.'phone_mobile']) && strlen($_POST[$prefix.'phone_mobile']) != 0 ) {
	$query .= " (phone_mobile='". $_POST[$prefix.'phone_mobile'] . "' AND last_name = '". $_POST[$prefix.'last_name'] ."')";
}
else if(isset($_POST[$prefix.'phone_home']) && strlen($_POST[$prefix.'phone_home']) != 0 ) {
	$query .= " (phone_home='". $_POST[$prefix.'phone_home'] . "' AND last_name = '". $_POST[$prefix.'last_name'] ."')";
}
else {
	$query .= " last_name= '". $_POST[$prefix.'last_name'] ."'";
}
return $query;

}

Please let us know if this worked for you, or anyone else that used this code