You need to sign in to do that
Don't have an account?
insert case comment of related case
i'm trying to insert case comments into related cases when the parent case inserts a new case comment and the public checkbox is checked.
For some reason only 1 related case gets a new case comment inserted eventhough there are 2.
Can someone help me PLEASE!!!! Thanks in advance!!!
trigger updateComment on CaseComment (after insert, after update)
{
Set<Id> parentCase=new Set<Id>();
Case> mapCase=new Map<Id,Case>(); Case> lstCase=[Select Id,ParentId From Case Where ParentId in :parentCase];
Map<Id,
for(CaseComment t: Trigger.new)
{
parentCase.add(t.ParentId);
}
List<
for(case c :lstCase)
{
mapCase.put(c.ParentId,c);
}
for(CaseComment t: Trigger.new)
{
CaseComment[] addCom = new CaseComment[0];
for(Case target : mapCase.values()){
if(mapCase.containsKey(t.ParentId) && t.isPublished == TRUE)
new CaseComment(CommentBody=t.CommentBody, ParentId=mapCase.get(t.ParentId).id, isPublished=TRUE));
}
insert addCom;
}
}
}
}
After tinkering almost all night i found a solution that worked.
trigger updateComment on CaseComment (after insert, after update)
{
Map<Id,CaseComment> cMap = new Map<Id,CaseComment>();
for (CaseComment t: Trigger.new){
cMap.put(t.ParentId,t);
}
Set<Id> idSet = cMap.keySet();
List<Case> allCases = [select Id,ParentId from Case where ParentId in :idSet];
List<CaseComment> childCom = new List<CaseComment>();
for(integer i=0;i<allCases.size();i++){
CaseComment newCom = new CaseComment();
newCom.CommentBody = cMap.get(allCases[i].ParentId).CommentBody;
newCom.IsPublished = TRUE;
newCom.ParentId = allCases[i].id;
childCom.add(newcom);
}
if(!childCom.isEmpty()){
insert childCom;
}