Avoid duplicate record in Contacts and Accounts during Lead conversion with Custom Function

Avoid duplicate record in Contacts and Accounts during Lead conversion with Custom Function

Hi Experts,
I having problem with custom function "zoho.crm.covertlead" command in my script.
I have a Lead A record about to convert, and the Contacts A is exist in the record.
During the Lead Convert it will auto generate become Contacts B but instead of become Contacts A. 
I did using the searchRecord to check and update the same, but still not working very well.
What i wish to achieve is when record is exist in Contacts A, during the convert lead, it will update on the same record in Contacts to avoid any duplicates.  If this cannot be done, please advise other workaround method?

Please kindly refer to my script:

convert = zoho.crm.convertLead(leadId,{"overwrite":true,"notify_lead_owner":false,"notify_new_entity_owner":false});
info "lead convert : " + convert;
//   Account name, auto pick Full_Name if Company name was empty
if(leadDetails.get("Company").Size() > 0)
{
Accname = leadDetails.get("Company");
}
else
{
Accname = leadDetails.get("Full_Name");
}
//
checkcont = zoho.crm.searchRecords("Contacts","(Email:equals:" + leadDetails.get("Email") + ")");
checkacct = zoho.crm.searchRecords("Accounts","(Account_Name:equals:" + Accname + ")");
// Using Full_Name 
if(convert.get("Accounts") == null)
{
// Check Existed Accounts 
if(checkacct.isEmpty() == false)
{
accname = checkacct.get(0).get("id");
info "accname : " + accname;
// Check Existed Contacts
if(checkcont.isEmpty() == false)
{
contname = checkcont.get(0).get("id");
info "Full Name : Exist Accounts & Exist Contacts";
}
else 
contname = convert.get("Contacts");
info "Full Name : Exist Accounts & New Contacts";
            }
update = zoho.crm.updateRecord("Contacts",contname,{"Account_Name":accname});
info "Update : " + update;
dealmap.put("Contact_Name",contname);
dealmap.put("Account_Name",accname);
invcontact = contname;
invaccount = accname;
}
else // To create New Accounts
{
createacct = zoho.crm.createRecord("Accounts",{"Account_Name":ifnull(leadDetails.get("Full_Name"),"")});
info "CreateAcct : " + createacct;
if(checkcont.isEmpty() == false) // Check if Exits Contacts
{
contname = checkcont.get(0).get("id");
info "Full Name : New Accounts & Exist Contacts";
}
else 
            {
contname = convert.get("Contacts");
info "Full Name : New Accounts & New Contacts";
            }
update = zoho.crm.updateRecord("Contacts",contname,{"Account_Name":createacct.get("id")});
info "Update : " + update;
dealmap.put("Contact_Name",contname);
dealmap.put("Account_Name",createacct.get("id"));
invcontact = contname;
invaccount = createacct.get("id");
}
}
else // When Company_Name has value 
{
// Check Exist Accounts
if(checkacct.isEmpty() == false)
{
acctname = checkacct.get(0).get("id");
if(checkcont.isEmpty() == false)
{
contname = checkcont.get(0).get("id");
info "Company Name : Exist Accounts & Exist Contacts";
}
else 
            { 
contname = convert.get("Contacts");
info "Company Name : Exist Accounts & New Contacts";
            }
update = zoho.crm.updateRecord("Contacts",contname,{"Account_Name":acctname});
info "updatet : " + update;
dealmap.put("Contact_Name",contname);
dealmap.put("Account_Name",acctname);
invcontact = contname;
invaccount = acctname;
}
else
{
// Update New Accounts
createacct = zoho.crm.createRecord("Accounts",{"Account_Name":ifnull(leadDetails.get("Company"),"")});
if(checkcont.isEmpty() == false)
{
contname = checkcont.get(0).get("id");
info "Company Name : New Accounts & Exist Contacts";
}
else 
            {
contname = convert.get("Contacts");
info "Company Name : New Account & New Contacts";
            }
update = zoho.crm.updateRecord("Contacts",contname,{"Account_Name":convert.get("Accounts")});
info "Update Account: " + update;
dealmap.put("Contact_Name",contname);
dealmap.put("Account_Name",convert.get("Accounts"));
invcontact = contname;
invaccount = convert.get("Accounts");
}
}
info "InvContact : " + invcontact;
info "InvAccount : " + invaccount;