0

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

4
  • Can you add the error you are receiving?
    – Jayant Das
    Apr 23, 2019 at 12:57
  • I have updated my question
    – Pavan tej
    Apr 23, 2019 at 13:11
  • Are you receiving error on this line -- ll.Parent_Lead__c=''; What is the type of Parent_Lead__c field?
    – Jayant Das
    Apr 23, 2019 at 13:15
  • It is lookup field to the lead object
    – Pavan tej
    Apr 23, 2019 at 13:16

1 Answer 1

1

Your issue is on this line:

ll.Parent_Lead__c='';

Based on your comments, Parent_Lead__c is a lookup field which can contain only Id values and that you are trying to assign an empty string to it, which is resulting in the error.

If you are creating a new record out there, you don't need to assign anything to this field, you can simply leave it as is without the need of setting a value out there. If you are re-parenting then you can assign a null value to the field.

2
  • I have updated the code. But it's taking too much time to convert even I don't know whether it's working or not
    – Pavan tej
    Apr 23, 2019 at 14:28
  • @Pavantej That will classify as a different question based on the format of SFSE. You may like to open a new one with all the applicable details.The answer to the current question was to address the specific issue that you were having.
    – Jayant Das
    Apr 23, 2019 at 14:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .