0

I have create lookup relationship between lead(parent)& case(child) and opportunity(parent)& case(child).When converting the lead, I would like to update the lead case field to the opportunity case filed.

This is my code

Trigger UpdaterecordDetails on Lead (after update){
    Map<Id, Lead> leadMap = new Map<Id,Lead>();
        for (Integer led = 0; led < Trigger.new.size(); led ++){
        if (Trigger.new[led].IsConverted == true && Trigger.old[led].isConverted == false) {
        leadMap.put( Trigger.new[led].Id, Trigger.new[led]);
        }
    List<Case> cases = [SELECT Id FROM Case];
    for(Case c: cases)
    {
        c.Opportunity_Name__c = Lead.ConvertedOpportunityId;
        cases.add(c);
    } 
    if(cases.size() > 0)
    {
        update cases;
    }
    }
}

I have the following error message: Illegal assignment from Schema.SObjectField to Id

This line shows the error: c.Opportunity_Name__c = Lead.ConvertedOpportunityId;

Please advise how to update the lookup field.

3 Answers 3

1

Lead.ConvertedOpportunityId is a static reference to an sObjectField token that represents that field. You need to get the value from leadMap somehow. Your current logic will assign every case in the database to a lead when it is converted, which is probably not what you want to do. You're going to need additional logic to figure out which case should be assigned to which opportunity based on the lead. As an aside, if(cases.size() > 0) is no longer necessary in modern Apex code.

0

You are not referring to a lead record, you are referring to the lead object in your line. Use the trigger map to get the lead records and then assign them. Hope that helps.

0

You need a variable of type Lead to store a Lead record. Lead.ConvertedOpportunityId is a static variable of type SObjectField for that particular field.

What you're missing is some logic to figure out which lead record corresponds to which case record that you want to update, and then:

Lead someLead = leadMap.get(someLeadId);
caseRecord.Opportunity_Name__c = someLead.ConvertedOpportunityId;
1
  • I can't really do that without understanding your use case. Right now you are just querying all cases, how do you determine which case record should be updated with the value of which lead record?
    – adriancg
    Aug 5, 2020 at 13:24

You must log in to answer this question.

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