2011-11-29 101 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