How to update related cases and fields from one recordtype to other recordtype in lead

点点圈 提交于 2021-01-29 06:52:10

问题


public class UpdaterecordtypeforLead {
    Public static void Leadtootherupdate() {
     List<Lead> OlderLead = [SELECT Id,Name, RecordTypeId from Lead where RecordType.Name = 'Enquiries'];
    List<Lead> NewLead = New List<Lead> ();
    If(NewLead.Id == null)
    {
       for( Lead l : OlderLead) {
          for(Lead l1 : NewLead) {
               l1.LastName = l.LastName;
               l1.Email= l.Email;
               NewLead.add(l1);
               NewLead.add(l1);
              Insert l1;
          }
       }
    
    If ( (l1. LastName == l.LastName && l1.Email == l.Email)) 
    {
          NewLead.add(OlderLead.Related_Cases__c);
          NewLead.add(l.Id);
          Update NewLead;
   }
} 
     
    }
}
     

I'm getting error while saving.


回答1:


I think you have a lot of issues:

  • What is the context / aim of this code ?
  • Query using DeveloperName not Name as it's language dependent
  • Testing if (NewLead.Id == null) when ``NewLead` is a list will not work
  • Looping on NewLead for what reason ?
  • Duplicate NewLead.add(l1)
  • Insert in a loop will gets you to governor limits and should be avoided, go for bulk insert
  • The if is outside the loop
  • Don't update something you just insert, instead make sure everything is okay before insert
  • NewLead is a list of Lead so you can't add OlderLead.Related_Cases__c or l.Id to it
  • Think about indenting correctly your code, it's easier for others to read
  • Use naming conventions

A fixed code would probably be something like this:

public class UpdateRecordtypeForLead {
    public static void leadToOtherUpdate () {
        List<Lead> olderLeads = [
            SELECT 
                Id,
                LastName,
                Email,
                RecordTypeId
            FROM 
                Lead 
            WHERE 
                RecordType.DeveloperName = 'Enquiries'
        ];
        List<Lead> newLeads = New List<Lead>();

        for (Lead olderLead : olderLeads) {
            newLeads.add(new Lead(
                Id = olderLead.Id,
                LastName = olderLead.LastName,
                Email = olderLead.Email,
                RecordTypeId = 'the_new_record_type_id'
            ));
          }
       }

       update newLeads;
   }
}


来源:https://stackoverflow.com/questions/63295574/how-to-update-related-cases-and-fields-from-one-recordtype-to-other-recordtype-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!