2011-11-29 96 views
0

我们已经建立了这个流程来帮助优先考虑我们的案例。要做到这一点,我希望通过电子邮件到个案触发新案例记录。然后查询它是否与帐户相关。如果是,我想返回一个值,cScore。然后插入。如何在案例记录成为案例时访问案例记录。 (Email2Case)

这是我迄今为止,它返回这个错误:

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger newCaseScoring caused an unexpected exception, contact your administrator: newCaseScoring: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.newCaseScoring: line 12, column 18

这里是我的代码:

触发newCaseTrigger案例(插入前){

if(Trigger.isInsert){ 
    List<Case> cList = [Select AccountId, Id, Priority_Score__c, Case_Priority_Score__c FROM Case WHERE Id IN :Trigger.new]; 
    List<Case> updateList = [Select AccountId, Id, Priority_Score__c, Case_Priority_Score__c FROM Case WHERE Id IN :Trigger.new]; 

    System.debug('This is the cList:' + cList); 
    integer size; 
    integer cnt=0; 
    List<id> idList = New List<id>(); 
    for(Case cases : cList){ 

     idList.add(cases.AccountId); 
     size = idList.size(); 
     System.debug('This is the idList:' + idList); 
     System.debug('This is the size:' + size);  
     cnt++; 
     System.debug(cnt); 
    } 
    List<Account> aList = [Select Id, Customer_s_Score_Total__c from Account where id =:idList]; 
    integer num; 
    for(Id a : aList){ 
     for(Id b : idList){ 
      if(aList.Id == idList){ 
       num = aList.Customer_s_Score_Total__c; 
       updateList.Priority_Score__c = num; 
      } 
     } 
    } 
} 

}

感谢您的帮助。

回答

5

您在第12行中的查询返回0条记录,因为该案例还没有Id。由于您处于before insert触发器中,此案件尚未提交。

我想你会想要做的是:

  1. 遍历Trigger.new的情况下,建设Case.AccountId值的List<Id>
  2. 使用上一步中的List<Id>进行帐户查询。
  3. 再次循环遍历案例,更新Case_Priority_Score__c字段。

你会不是需要做一个插入调用。您对案例记录所做的任何更改都会自动保存,因为您处于before insert触发器中。

+0

谢谢你的经历。我更新了我的代码。这不是一个工作模式,我只是想把它扔到那里,以确保我抓住了这个愿景。我做得对吗? – Havoc783

+1

我不认为你需要查询案件。 'for(case cases:cList){'can become'for(Case cases:Trigger.new){'。如果你要为你的账户建立一个Map,你不需要做循环内循环。尝试'地图 aMap =新地图([Select Id,Customer_s_Score_Total__c from Account where IN:idList]);' –

+0

好吧,谢谢,我会试试。谢谢你的帮助! – Havoc783