我想写一个批量触发器(牢记管控器限制),这样当插入一个'x‘字段值与某个opporunity的'y’字段值相同的新lead时,它会更新此opportunity的字段'z‘。
我刚接触salesforce和顶峰,所以面临着问题。
任何帮助都将不胜感激。
谢谢
Jitendra
发布于 2011-05-05 07:49:23
你有开发经验吗?你在处理多少条记录?如果你有超过10k的机会,那么你很快就会遇到问题!
如果你是一名开发人员,下面的(未经测试的)代码应该是有意义的,但请记住,你只能执行20个SOQL查询,允许19个更新调用,每个调用200条记录。如果你需要更多的机会,那么你需要编写一个使用Batchable接口的类,并从触发器中触发它--在调用- so之前,你需要通过X值的列表,这样你就可以在查询中使用它们(参见here for the docs on Batch Apex。
trigger LeadAfterInsert on Lead (after insert)
{
// assuming string for the type of fields X & Y
set<string> setXValues = new set<string>();
list<Opportunity> liOpptysToUpdate = new list<Opportunity>();
for(Lead sLead : trigger.new)
{
setXValues.add(sLead.FieldX);
}
for(Opportunity [] sOpportunityArr : [select Id, FieldZ, FieldY
from Opportunity
where FieldY in : setXValues
limit 1000])
{
for(Opportunity sOpportunity : sOpportunityArr)
{
// field logic here, e.g.
if(sOppty.FieldY != 0)
{
sOppty.FieldZ ++;
}
liOpptysToUpdate.add(sOppty);
// can only update 200 records at once so we check the list size here
if(liOpptysToUpdate.size() == 200)
{
update liOpptysToUpdate;
liOpptysToUpdate.clear();
}
}
}
// deal with any stragglers
if(liOpptysToUpdate.size() > 0)
{
update liOpptysToUpdate;
}
}https://stackoverflow.com/questions/5886436
复制相似问题