本文介绍了如何在线索中将相关案例和字段从一种记录类型更新为另一种记录类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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;
}
}
}
}
保存时出现错误.
推荐答案
我认为您有很多问题:
- 此代码的上下文/目的是什么?
- 使用
DeveloperName
而不是Name
进行查询,因为它取决于语言 - 在列表中测试
if(NewLead.Id == null)
无效 - 出于什么原因而选择NewLead?
- 重复
NewLead.add(l1)
- 插入循环将使您达到调速器限制,应避免使用此方法进行批量插入
-
if
是否在循环之外 - 不要更新您刚刚插入的内容,而是请确保在插入之前一切正常
-
NewLead
是Lead
的列表,因此您不能添加OlderLead.Related_Cases__c
或l.Id
- 考虑正确缩进代码,其他人更容易阅读
- 使用命名约定
- 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;
}
}
这篇关于如何在线索中将相关案例和字段从一种记录类型更新为另一种记录类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!