问题
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
notName
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 ofLead
so you can't addOlderLead.Related_Cases__c
orl.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