Currently, I'm working on the parent-child relationship on the lead object. So, we are restricting users to convert only master lead & then top 20 related leads will be converting as contacts to the same account.
And we want to update existing unwanted related leads to mark the status as not required & update other fields too.
public class RelatedContacts
{
public Static List<Contact> Relatedcontacts = new List<Contact>();
public static integer count=0;
public Static List<Lead> OtherLeads = new List<Lead>();
public static void LeadConvertion(Map<Id,Id> RelatedLeads)
{
List<Database.LeadConvert> converts = new List<Database.LeadConvert>();
String convertedStatus = [SELECT MasterLabel FROM LeadStatus WHERE IsConverted = TRUE LIMIT 1].MasterLabel;
for(Lead Ls:[Select id,Parent_Lead__c from lead where Parent_Lead__c=:RelatedLeads.keyset()])
{
If(Ls.Parent_Lead__c <> null && count <20)
{
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(Ls.Id);
lc.setConvertedStatus(convertedStatus);
lc.setAccountId(RelatedLeads.get(Ls.Parent_Lead__c));
lc.setDoNotCreateOpportunity(true);
converts.add(lc);
count =count+1;
}
if(Ls.Parent_Lead__c <> null && count >=20)
{
//Lead lss=new lead();
Lead ll=new Lead(Id = Ls.Id);
//ll.id=Ls.id;
Count=Count+1;
//ll.Status='Not Visible';
ll.Parent_Lead__c='';
OtherLeads.add(ll);
//OtherLeads.add(lss);
}
}
If(!OtherLeads.isEmpty())
{
Update OtherLeads;
}
Database.convertLead(converts, false);
}
}
My Problem is: when I try to convert a lead I'm getting this error.
5:28:05.55 (2030963109)|DML_END|[1738] 05:28:05.55 (2031063455)|EXCEPTION_THROWN|[1738]|System.DmlException: Update failed. First exception on row 0 with id 00Q0p000001FrJvEAK; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateTerritories: execution of AfterUpdate
caused by: System.StringException: Invalid id:
Class.RelatedContacts.LeadConvertion: line 42, column 1 Trigger.UpdateTerritories: line 110, column 1: [] 05:28:05.55 (2031459688)|HEAP_ALLOCATE|[1738]|Bytes:322
ll.Parent_Lead__c='';
What is the type ofParent_Lead__c
field?