function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Roopa S 1Roopa S 1 

Need to write trigger

i have Created a field on account as 'summary'(text field). need to Write a trigger for updating the account summary whenever the description of any related case is changed, the account summary be the concatenation of all the related cases
Best Answer chosen by Roopa S 1
CharuDuttCharuDutt
Hii Roopa
try below trigger
if AccountId is Not Null And Description Is Changed than It Will update
trigger NumberOfChild on Case (After Update) {
   List<Account> accList=new List<Account>();
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Case con : Trigger.new){ 
            if(con.AccountId != null && con.Description != Trigger.oldMap.get(con.Id).Description ){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    
    for(Account acc :[Select id,summary__c ,(Select id,CaseNumber,Description from Cases) from Account where Id in : setAccIds]){
      String s ='';
        for(Case Con :acc.Cases){
            s+=Con.Description +' , ';
        }
        acc.summary__c =  s.removeEnd(',');
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Roopa
Try Below Trigger
trigger NumberOfChild on Case (After Insert,After Update,After Delete) {
   List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Case con : Trigger.new){
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Case con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Case con : Trigger.old) { 
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,summary__c ,(Select id,CaseNumber from Cases) from Account where Id in : setAccIds]){
      String s ='';
        for(Case Con :acc.Cases){
            s+=Con.CaseNumber +',';
        }
        acc.summary__c =  s.removeEnd(',');
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
CharuDuttCharuDutt
Hii Roopa
Try Below Trigger
trigger NumberOfChild on Case (After Insert,After Update,After Delete) {
   List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Case con : Trigger.new){
            if(con.AccountId != null && con.Description != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Case con : Trigger.new){ 
            if(con.AccountId != null && con.Description != Trigger.oldMap.get(con.Id).Description ){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Case con : Trigger.old) { 
            if(con.AccountId != null && con.Description != null){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    }    
    for(Account acc :[Select id,summary__c ,(Select id,CaseNumber from Cases) from Account where Id in : setAccIds]){
      String s ='';
        for(Case Con :acc.Cases){
            s+=Con.CaseNumber +',';
        }
        acc.summary__c =  s.removeEnd(',');
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
Roopa S 1Roopa S 1
@CharuDutt
we have to write trigger whenever the description of any related case is changed, the account summary be the concatenation of all the related cases.
example-
(account)summary= desc of case 1 + desc of case2 + desc of case3(case)
CharuDuttCharuDutt
Hii Roopa
try below trigger
if AccountId is Not Null And Description Is Changed than It Will update
trigger NumberOfChild on Case (After Update) {
   List<Account> accList=new List<Account>();
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Case con : Trigger.new){ 
            if(con.AccountId != null && con.Description != Trigger.oldMap.get(con.Id).Description ){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
          
			}        
        }
    }
    
    for(Account acc :[Select id,summary__c ,(Select id,CaseNumber,Description from Cases) from Account where Id in : setAccIds]){
      String s ='';
        for(Case Con :acc.Cases){
            s+=Con.Description +' , ';
        }
        acc.summary__c =  s.removeEnd(',');
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As best Answer If It Helps
Thank You!
This was selected as the best answer