Salesforce 需要帮助克服超出Apex CPU时间限制的错误
我正在开发一个模块,该模块将在插入/更新时消除重复的联系人记录,但我遇到了超出Apex CPU时间限制的错误。我知道要克服它,我们需要对代码进行一点优化,但在下面的代码块中,优化它的范围似乎很小。任何帮助都将不胜感激 根据帐户,我们有一个mulipicklist,从中我们可以选择字段,根据这些字段定义此帐户下联系人的唯一性。它可以因差异而不同。下面的代码是trigger处理程序类的一部分,其中我们有一个帐户Id为key的旧联系人列表映射(MapOfAccountId WithItsContact)和一个新联系人列表和帐户为key的映射(newContactWithAccountMap)。我们根据在triiger中获得的联系人的帐户集迭代这些映射。我们有一个用于每个帐户联系人唯一性的字段列表映射(MapOfcountWithFilters) 以下是代码片段:Salesforce 需要帮助克服超出Apex CPU时间限制的错误,salesforce,apex-code,visualforce,apex,force.com,Salesforce,Apex Code,Visualforce,Apex,Force.com,我正在开发一个模块,该模块将在插入/更新时消除重复的联系人记录,但我遇到了超出Apex CPU时间限制的错误。我知道要克服它,我们需要对代码进行一点优化,但在下面的代码块中,优化它的范围似乎很小。任何帮助都将不胜感激 根据帐户,我们有一个mulipicklist,从中我们可以选择字段,根据这些字段定义此帐户下联系人的唯一性。它可以因差异而不同。下面的代码是trigger处理程序类的一部分,其中我们有一个帐户Id为key的旧联系人列表映射(MapOfAccountId WithItsContact
for(String accountId : accountIdSet){
if(newContactWithAccountMap.get(accountId) != null){
for(Contact newContact : newContactWithAccountMap.get(accountId)){
for(Contact oldContact : mapOfAccountIdWithItsContact.get(accountId)){
//Check for duplication only in the respective account, also this should not apply on insertion of Office contact
matchingContactFound = false;
if(oldContact.id != newContact.id){ //while insert, newContact's id will be null and while update it will verify that it is not matching itself with its old record.
for(String filterFieldName : mapOfAccountWithFilters.get(accountId)){
if(oldContact.get(filterFieldName) == newContact.get(filterFieldName)){
matchingContactFound = true;
//If match is found update last de duplication date to today on old contact
oldContact.Last_De_Duplication_Date__c = System.Today();
oldContactsToUpdateSet.add(oldContact);
}else{
matchingContactFound = false;
break; //get another "old contact"
}
}
}
if(matchingContactFound){
//stop it from being inserted
duplicateContactSet.add(newContact.Id);
//newContact.addError('Contact cannot be inserted because a contact is already present based on the Master Target Identifier at client level.');
break; //get another "new contact"
}
}
}
}
}
任何避免4个循环或替代方法的帮助都将不胜感激。提前谢谢。好问题
但是,如果不看到更多的代码,很难说出一些东西
Map答案的一部分可能是将其作为批处理。当运行批处理代码时,处理的每个单独的批记录都有其自己的时间限制。有关批处理的更多信息
您不会显示SOQL查询,也不会给出一个帐户可能有多少联系人的想法。帐户和联系人的总数也会很有用。如果没有这些,就很难确定问题可能出在哪里。您可能需要查看SOQL优化。例如,有像“not equals”(“!=”)这样的运算符这是低效的。关于这个话题还有很多可以说的,你可以阅读
另一种可能性是,您可以用一个查询替换多个查询。在类似的情况下,我可以编写代码,对每个站点(自定义对象)执行一个查询。这会将我限制为100个站点。相反,我对所有站点执行一个查询(我们的限制远远低于50000个)然后通过一个唯一的键创建了一个映射来存储查询结果。这是我见过的最好的代码回顾。你太棒了!嘿@MnZ,thx:)这是你在这里做的一些严肃的问题巫术。。。我再也没有这样的空闲时间了,但我几乎每天都在继续节省我的时间。我很高兴“我回馈了社区”(听起来很奇怪),当我可以的时候!