# Help required to understand code section from mautic docs for creating lead and preventing duplicate

**URL:** <https://forum.mautic.org/t/help-required-to-understand-code-section-from-mautic-docs-for-creating-lead-and-preventing-duplicate/3548>\
**Category:** Development\
**Created:** [April 20, 2016, 8:24am UTC](https://forum.mautic.org/t/help-required-to-understand-code-section-from-mautic-docs-for-creating-lead-and-preventing-duplicate/3548 "2016-04-20T08:24:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiwari.mayank24](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@tiwari.mayank24](https://forum.mautic.org/u/tiwari.mayank24)\
**Post date:** [April 20, 2016, 8:24am UTC](https://forum.mautic.org/t/help-required-to-understand-code-section-from-mautic-docs-for-creating-lead-and-preventing-duplicate/3548/1 "2016-04-20T08:24:53Z")

</div>

**[h]Can somebody help me understand this code ?[/h]** It can be found on the doc page [https://developer.mautic.org/#manipulating-leads](https://developer.mautic.org/#manipulating-leads) In creating new leads section.  
  
When I paste this code in the code editor it gives me error, $availableLeadFields is not defined, and to be true I can’t see where it is defined and what is its use, I understand it is being used to compare the unique keys in fields, but How ?  
  
  
  
I’ll be great full if anyone can help me understand this code, it’ll help a lot, I am making automated plugins and cron commands to help import leads from Google Sheet to Mautic, but every time there are a lot of duplicate entries I want to use this code to prevent duplicates, any help is appreciated. Thank you in advance.

Code:

// Optionally check for identifier fields to determine if the lead is unique $uniqueLeadFields = $this-\>factory-\>getModel('lead.field')-\>getUniqueIdentiferFields(); $uniqueLeadFieldData = array();

// Check if unique identifier fields are included  
$inList = array\_intersect\_key($leadFields, $availableLeadFields);  
foreach ($inList as $k =\> $v) {  
if (empty($query[$k])) {  
unset($inList[$k]);  
}

```
if (array_key_exists($k, $uniqueLeadFields)) {
    $uniqueLeadFieldData[$k] = $v;
}

```

}

// If there are unique identifier fields, check for existing leads based on lead data  
if (count($inList) && count($uniqueLeadFieldData)) {  
$existingLeads = $this-\>em-\>getRepository(‘MauticLeadBundle:Lead’)-\>getLeadsByUniqueFields(  
$uniqueLeadFieldData,  
$leadId // If a currently tracked lead, ignore this ID when searching for duplicates  
);  
if (!empty($existingLeads)) {  
// Existing found so merge the two leads  
$lead = $leadModel-\>mergeLeads($lead, $existingLeads[0]);  
}

```
// Get the lead's currently associated IPs
$leadIpAddresses = $lead->getIpAddresses();

// If the IP is not already associated, do so (the addIpAddress will automatically handle ignoring
// the IP if it is set to be ignored in the Configuration)
if (!$leadIpAddresses->contains($ipAddress)) {
    $lead->addIpAddress($ipAddress);
}

```

}

---

<div class="post-metadata">

**Author:** ![tiwari.mayank24](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@tiwari.mayank24](https://forum.mautic.org/u/tiwari.mayank24)\
**Post date:** [April 20, 2016, 8:24am UTC](https://forum.mautic.org/t/help-required-to-understand-code-section-from-mautic-docs-for-creating-lead-and-preventing-duplicate/3548/2 "2016-04-20T08:24:53Z")

</div>

[h]Can somebody help me understand this code ?[/h] It can be found on the doc page [https://developer.mautic.org/#manipulating-leads](https://developer.mautic.org/#manipulating-leads) In creating new leads section.  
When I paste this code in the code editor it gives me error, $availableLeadFields is not defined, and to be true I can’t see where it is defined and what is its use, I understand it is being used to compare the unique keys in fields, but How ?

I’ll be great full if anyone can help me understand this code, it’ll help a lot, I am making automated plugins and cron commands to help import leads from Google Sheet to Mautic, but every time there are a lot of duplicate entries I want to use this code to prevent duplicates, any help is appreciated. Thank you in advance.

```auto
// Optionally check for identifier fields to determine if the lead is unique
$uniqueLeadFields = $this->factory->getModel('lead.field')->getUniqueIdentiferFields();
$uniqueLeadFieldData = array();

// Check if unique identifier fields are included
$inList = array_intersect_key($leadFields, $availableLeadFields);
foreach ($inList as $k => $v) {
    if (empty($query[$k])) {
        unset($inList[$k]);
    }

    if (array_key_exists($k, $uniqueLeadFields)) {
        $uniqueLeadFieldData[$k] = $v;
    }
}

// If there are unique identifier fields, check for existing leads based on lead data
if (count($inList) && count($uniqueLeadFieldData)) {
    $existingLeads = $this->em->getRepository('MauticLeadBundle:Lead')->getLeadsByUniqueFields(
        $uniqueLeadFieldData,
        $leadId // If a currently tracked lead, ignore this ID when searching for duplicates
    );
    if (!empty($existingLeads)) {
        // Existing found so merge the two leads
        $lead = $leadModel->mergeLeads($lead, $existingLeads[0]);
    }

    // Get the lead's currently associated IPs
    $leadIpAddresses = $lead->getIpAddresses();

    // If the IP is not already associated, do so (the addIpAddress will automatically handle ignoring
    // the IP if it is set to be ignored in the Configuration)
    if (!$leadIpAddresses->contains($ipAddress)) {
        $lead->addIpAddress($ipAddress);
    }
}
```
