2017-03-01 69 views
-1

當用戶點擊case來更改caseOwner/user時,我必須將此用戶添加到所有關聯的案例(每個客戶)。我正在使用下面的代碼,但它只更新一個記錄/案例。我可以看到在調試日誌中,所有案例都用最新的所有者更新,但沒有真正更新/存儲在caseObject中。請幫助我。Apex觸發器 - 如何使用觸發器更新所有案例所有者?

trigger caseAssignment on Case (after insert, after update) { 

     set<id> ownerId = new Set<Id>(); 
     set<id> customerId = new set<Id>(); 
    for(Case caseobj : trigger.new){ 

      ownerId.add(caseobj.OwnerId); 
      customerId.add(caseobj.AccountId); 
    } 


for(User user:[Select id, FirstName, LastName from user where Id IN :ownerId]){ 

    for(Case cas : [Select Id, OwnerId, First_Name__c, CaseNumber, AccountId From Case where AccountId IN: customerId]){   

     cas.OwnerId = user.Id; 

    } 


} 

回答

0

沒有更新的DML語句,您的代碼將不會對您的數據產生任何影響。

或許應該在你的內心創造一個更新列表

即列表updateCaseList =新名單()

然後for循環,增加「中科院」以updateCaseList,然後在方法結束時,發出的更新命令(更新updateCaseList)。

0

假設你寫的邏輯是正確的你永遠更新的情況下

trigger caseAssignment on Case (after insert, after update) 
{ 

    Set<Id> ownerId = new Set<Id>(); 
    Set<Id> customerId = new Set<Id>(); 
    List<Case> casesToUpdate = new List<Case>(); 

    for(Case caseobj : trigger.new) 
    { 
     ownerId.add(caseobj.OwnerId); 
     customerId.add(caseobj.AccountId); 
    } 


    for(User user:[Select id, FirstName, LastName from user where Id IN :ownerId]) 
    { 
     for(Case cas : [Select Id, OwnerId, First_Name__c, CaseNumber, AccountId From Case where AccountId IN: customerId]) 
     {   
      Case c = new Case(); 
      c.ID = cas.ID; 
      c.OwnerId = user.Id; 
      casesToUpdate.add(c); 
     } 
    } 

    update casesToUpdate; 
}