You need to sign in to do that
Don't have an account?
Vidya H 4
how to write batch class for this
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,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;
}
}
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,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;
}
}
Hope you are doing good
I have written a Test class for your requirement, please follow the below code for your reference.
Hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too
Thanks and regards
Suraj Tripathi
All Answers
Hope you are doing good
I have written a Test class for your requirement, please follow the below code for your reference.
Hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too
Thanks and regards
Suraj Tripathi