You need to sign in to do that
Don't have an account?
Nish321
Trigger to create a case comment when a case is inserted
Can anyone help me out with this trigger? I would like to create a new case comment whenever a case is inserted or updated. Below is the trigger i've written:
trigger UpdateanotherObj on Case (after insert, after update) {
//whenever a case is created a student should also be created
set <ID> Caseids = new set<ID> ();
list<casecomment> casecommentlist = new list <casecomment> ();
// casecomment[] comments = new casecomment[0];
// database.dmloptions option = new database.dmloptions();
for (case c : trigger.new)
{
caseids.add(c.id);
}
// list<casecomment> cc = [select ID, ParentID from CaseComment where ParentID IN : caseids] ;
for(case c : trigger.new )
{
casecomment cc = new casecomment();
cc.parentid = c.ID;
cc.commentbody = 'case comment this is';
cc.ispublished = true;
casecommentlist.add(cc);
}
insert casecommentlist;
}
trigger UpdateanotherObj on Case (after insert, after update) {
//whenever a case is created a student should also be created
set <ID> Caseids = new set<ID> ();
list<casecomment> casecommentlist = new list <casecomment> ();
// casecomment[] comments = new casecomment[0];
// database.dmloptions option = new database.dmloptions();
for (case c : trigger.new)
{
caseids.add(c.id);
}
// list<casecomment> cc = [select ID, ParentID from CaseComment where ParentID IN : caseids] ;
for(case c : trigger.new )
{
casecomment cc = new casecomment();
cc.parentid = c.ID;
cc.commentbody = 'case comment this is';
cc.ispublished = true;
casecommentlist.add(cc);
}
insert casecommentlist;
}
list<casecomment> cc = [select ID, ParentID from CaseComment where ParentID IN : caseids] ;
For(case c:trigger.new)
{
if(cc.parentid==c.id)
cc.add(new casecomment.commentbody('case comment this is'));
}
insert cc;
Your trigger works well . what ever you written is correct. I have removed the unnessary code and comment lines. try with below code.
Regards,
Sai Krishna Tavva.
Please find the below modified trigger:
Please do let me know if it helps you.
Regards,
Mahesh
Please make sure that you adding few more conditions in case of update scenario as it will always creates a Case Comments for every update.
I also modified the trigger to consider:
(1) Naming Convention.
(2) Alignment.
(3) Bulkified the trigger.
(4) I already tested the above code and it is working fine.
Please do let me know if it helps you.
Regards,
Mahesh