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
Rohit SharmaGRohit SharmaG 

Task creation for other User via trigger

Hi All,

I want to assign task to user, Not to lead/contact .  Case Comment and Case has master details relationship.


trigger TaskForCaseCommentTrigger on Case_Comment__c (after insert)
{
  List<Task> newTasks = new List<Task>();

  //Loop through all records in the Trigger.new collection
  //for(Case_Comment__c cc: Trigger.new)
  
  
   for(integer i=0; i<trigger.new.size(); i++)
  {   
    //List<Case> cases = [select id,Owner.Id from Contact where accountId = :a.Id];
       system.debug('Owner ID----'+trigger.new[i].Case__r.OwnerId);
      
       newTasks.add(new Task(
            ActivityDate = Date.today(),
            WhoID = trigger.new[i].Case__r.OwnerId,
            Status = 'Not Started',
            type = 'Other',
            Priority = 'Normal',
            Subject = 'Case Comment Added',
            description = trigger.new[i].Comment__c
        ));
   }
   insert newTasks ;
}


Virendra ChouhanVirendra Chouhan
Hi Rohit,

I think you have to use Owner field of Task object.
and assign the user's id on that.

Reagards
Virendra