function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashish YelmuleAshish Yelmule 

Please Help me to fix this

trigger LeadConversionTrigger on Lead( after update)

{

for( Lead l : Trigger.new )

{

// Check if the Lead is converting. 

//Once Lead is converted, Lead is read-only so this can only happen once 

if ( l.isConverted )

{

// Obtain a list of cases associated to the Lead

List< Case > CasesToConvert = [SELECT Id, Lead__c, ContactId, AccountId FROM Case WHERE Lead__c = l.Id];

 

// Update the Cases associated to the Lead to its converted Contact and Account Id

if ( CasesToConvert.size() > 0 )

{

for ( Case c : CasesToConvert )

{

c.ContactId = l.ConvertedContactId;

c.AccountId = l.ConvertedAccountId;

}

 

// Update the Cases

update CasesToConvert;

}

}

}

}


 
Best Answer chosen by Ashish Yelmule
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Ashish,

Yes, I missed it. I have updated the code now and has put some additional check as well.

If above solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Ashish,

Can you update the code as below. It workedn.
trigger LeadConversionTrigger on Lead( after update)
{
    list<Case> caselist= new List<Case>();
    map<id,Lead> leadmap= new map<id,Lead>();
    Set<id> leadid= new set<id>();
for( Lead l : Trigger.new ){
if ( l.isConverted )
{
leadid.add(l.id);
leadmap.put(l.id,l);
}
}
List< Case > CasesToConvert = [SELECT Id, Lead__c , ContactId, AccountId FROM Case WHERE Lead__c  in :leadid];
if ( CasesToConvert.size() > 0 )
{

for ( Case c : CasesToConvert )

{
if(leadmap.containsKey(c.Lead__c) )
{
c.ContactId = leadmap.get(c.Lead__c).convertedContactid;

c.AccountId = leadmap.get(c.Lead__c).convertedAccountid;
    caselist.add(c);
}
}

}
    if(caselist.size()>0)
    {
update caselist;
    }
}



If this solution helps, Please mark it as best answer.

Thanks,
Ashish YelmuleAshish Yelmule
@Sai Praveen  DML is supposed to be outside for each loop!
please correct me 

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Ashish,

Yes, I missed it. I have updated the code now and has put some additional check as well.

If above solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Ashish YelmuleAshish Yelmule

please provide me an email id for further communication

 

 

thank you