Due to the fact that my Deals conversion needs to be done in 2 ways - depending on the fact if those records already exists or not - resources in fields are different.
I am making function control conversation for Leads but I have problem with transferring email communication to Deal.
Here's a detailed explanation of the code and its functionality:
/**
* Function: LeadAktualizacjaKontaktFirmaPrzeniesienieDzialanNaDealaV1
* Purpose: Converts a Lead to a Deal and transfers all related emails to the new Deal
*
* Main operations:
* 1. Creates a new Deal from Lead data
* 2. Finds all emails related to the Lead
* 3. Updates email associations to point to the new Deal
*
* @param leadId - The ID of the Lead to be converted
*/
void automation.LeadAktualizacjaKontaktFirmaPrzeniesienieDzialanNaDealaV1(String leadId)
{
try
{
// Deklaracja wszystkich zmiennych
leadData = null;
kontakt = null;
firma = null;
dealName = null;
dealOwner = null;
leadOwner = null;
dealMap = Map();
newDeal = null;
dealId = null;
emailList = null;
emailIdVal = null;
updateMap = null;
updateResult = null;
eml = null;
// Pobranie danych leada
leadData = zoho.crm.getRecordById("Leads",leadId);
if(leadData.isEmpty())
{
info "BŁĄD: Lead o ID " + leadId + " nie został znaleziony.";
return;
}
kontakt = leadData.get("Kontakt_w_bazie");
firma = leadData.get("Firma_w_bazie");
dealName = leadData.get("Pods_Nazwa_Firmy");
dealOwner = leadData.get("Dla_Salesa");
leadOwner = leadData.get("Owner");
dealMap.put("Deal_Name",dealName);
dealMap.put("Account_Name",firma.get("id"));
dealMap.put("Contact_Name",kontakt.get("id"));
dealMap.put("Stage","Spotkania");
dealMap.put("Pipeline","Standard");
dealMap.put("Owner",dealOwner);
dealMap.put("Created_By",leadOwner);
dealMap.put("Lead_provided_by",leadOwner);
if(leadData.get("Lead_Source") != null)
{
dealMap.put("Lead_Source",leadData.get("Lead_Source"));
}
if(leadData.get("TTP_rezultat") != null)
{
dealMap.put("TTP_result",leadData.get("TTP_rezultat"));
}
if(leadData.get("Istniejaca_centrala") != null)
{
dealMap.put("Centrala",leadData.get("Istniejaca_centrala"));
}
if(leadData.get("E_hardware") != null)
{
dealMap.put("Wartosc_hardware",leadData.get("E_hardware"));
}
info "========= START TWORZENIA DEALA I PRZENOSZENIA MAILI =========";
info "Rozpoczęto dla leada: " + leadId;
info "Tworzę Deal z danymi: " + dealMap.toString();
newDeal = zoho.crm.createRecord("Deals",dealMap);
if(newDeal.get("id") == null)
{
info "BŁĄD: Nie udało się stworzyć Deala";
return;
}
dealId = newDeal.get("id");
info "✓ Utworzony Deal ID: " + dealId;
info "--- Przenoszenie emaili ---";
emailList = zoho.crm.searchRecords("Emails","(Lead_Id:equals:" + leadId + ")");
if(emailList != null && !emailList.isEmpty())
{
info "Debug: Liczba znalezionych emaili: " + emailList.size();
for each eml in emailList
{
try
{
if(eml != null)
{
info "Debug: Struktura emaila: " + eml.toString();
// Próbujemy pobrać ID bezpośrednio
emailIdVal = eml.get("id");
if(emailIdVal != null)
{
updateMap = Map();
updateMap.put("What_Id",dealId);
updateMap.put("$se_module","Deals");
info "Debug: Próba aktualizacji emaila o ID: " + emailIdVal;
updateResult = zoho.crm.updateRecord("Emails",emailIdVal,updateMap);
if(updateResult != null && updateResult.get("id") != null)
{
info "✓ Email " + emailIdVal + " przeniesiony";
}
else
{
info "Błąd: Nie udało się zaktualizować emaila " + emailIdVal;
}
}
else
{
info "Błąd: Nie znaleziono ID dla emaila";
}
}
}
catch(e)
{
info "BŁĄD przenoszenia emaila: " + e.toString();
info "Debug: Szczegóły emaila powodującego błąd: " + eml.toString();
}
}
}
else
{
info "Nie znaleziono żadnych emaili do przeniesienia";
}
info "========= ZAKOŃCZONO TWORZENIE DEALA I PRZENOSZENIE MAILI =========";
}
catch (ex)
{
info "Wystąpił błąd główny: " + ex.toString();
}
}
Here's what each part of the code does:
1. **Lead Data Retrieval and Deal Creation**:
- Retrieves Lead data using `zoho.crm.getRecordById("Leads", leadId)`
- Extracts important fields: Contact, Company, Deal Name, Deal Owner, etc.
- Creates a Deal Map with required fields:
```javascript
dealMap.put("Deal_Name", dealName);
dealMap.put("Account_Name", firma.get("id"));
dealMap.put("Contact_Name", kontakt.get("id"));
dealMap.put("Stage", "Spotkania");
dealMap.put("Pipeline", "Standard");
// ... other fields
```
- Creates a new Deal record using `zoho.crm.createRecord("Deals", dealMap)`
2. **Email Migration**:
- Searches for related emails using `searchRecords`:
```javascript
emailList = zoho.crm.searchRecords("Emails", "(Lead_Id:equals:" + leadId + ")");
```
- This search should return all emails associated with the Lead
3. **Email Association Update**:
- For each found email, attempts to:
- Get the email's ID
- Create an update map with new Deal association:
```javascript
updateMap.put("What_Id", dealId);
updateMap.put("$se_module", "Deals");
```
- Update the email record to point to the new Deal using `zoho.crm.updateRecord("Emails", emailIdVal, updateMap)`
**Current Issues**:
1. The email search returns records (4 emails found), but we're having trouble accessing the email ID field
2. The update operation fails due to data type issues with the email ID
**Requirements for Zoho Support**:
1. Need to confirm the correct field name for email ID in the search results
2. Need guidance on the correct way to update email associations when converting from Lead to Deal
3. Need to understand if there are any specific data type requirements for the email ID when updating records
**Test Results**:
========= START DEAL CREATION AND EMAIL TRANSFER =========
Started for lead: 751364000004048409
Creating Deal with data: {"Deal_Name":"Account Name","Account_Name":"751364000002639001","Contact_Name":"751364000004048395","Stage":"Meetings","Pipeline":"Standard","Owner":{"name":"Artur Sznek","id":"751364000000551001"},"Created_By":{"name":"Name","id":"751364000000416001","email":"
k******.*****@*****.com"},"Lead_provided_by":{"name":"Name","id":"751364000000416001","email":"
k******.*****@*****.com"},"TTP_result":232,"Central":"234","Hardware_value":72364}
✓ Created Deal ID: 751364000004185020
--- Email Transfer ---
Debug: Number of emails found: 4
ERROR transferring email: Error at line : 66
ERROR transferring email: Error at line : 69
========= DEAL CREATION AND EMAIL TRANSFER COMPLETED =========
Could you please help with:
1. The correct way to access email IDs from search results
2. The proper data type for email IDs when updating records
3. Any specific requirements for updating email associations in Zoho CRM
The code is functional for Deal creation but needs assistance with the email transfer portion.
Thank you in advance for help
kochnik