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
CharlieLangCharlieLang 

Apex trigger

I am currently trying to create a case comment when a task is added to a case.

 

I currently have the following trigger

 

trigger CMCreateCase on Task (after insert) {
    CaseComment cc = new CaseComment(CommentBody='Test',parentID=caseId);
    insert cc;
}

 How do i find the case ID in the task record

Best Answer chosen by Admin (Salesforce Developers) 
LakshmanLakshman

Modify your trigger as below:

 

trigger CMCreateCase on Task (after insert) {
    for(Task t: Trigger.new)
    {
    String caseId = t.WhatId;
    if(caseId.substring(0,2) == '500')
    {
    CaseComment cc = new CaseComment(CommentBody='Test',parentID=caseId);
    insert cc;
    }
    }
}

Let me know if this works.

 

Regards,

Lakshman