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
SatoriSatori 

Trigger to assign task to non-owner

 

We have a custom object (LOT) and on this object we have a field default case owner (This is a string, populated by a seperate trigger)
We need to create two tasks
  1. due in 45 days  from the close date
  2. due in 11 months from the close date
Each need to be assigned to the default case owner when the record meets a specific set of criteria (closed date is populated, status = closed)
I do not know how to code this and our developers are not familiar with SFDC and are running into issues.   
 
Can anyone provide some assistance, I'd be very greatful!!!!!
magicforce9magicforce9

Hi,

 

What does the 'default case owner' field have in string format, Name of the user or 15/18 digit salesforce ID ?

 

I have written some code that can help you get started with your requriement, assuming that the 'default case owner' field has the salesforce ID of a user + status and subject values and you may need to change it.

trigger LotTaskAssignmentTrigger on LOT__c(before update){
    
    List<task> tasksTobeInserted = new List<task>();

    for(Lot__c lot : trigger.new)
    {

        if(lot.Close_Date__c != null && lot.Status__c == 'Closed' && lot.Status__c != trigger.oldMap.get(lot.id).Status__c)
        {
            //Task1 which is due in 45 days from close date
            task task1 = new task(WhatId = lot.id, WhoId = lot.default_case_owner__c,
                                ActivityDate = lot.Close_Date__c.addDays(45), 
                                Status = 'In Progress', 
                                Subject = 'Call');
            tasksTobeInserted.add(task1);

            //Task2 which is due in 11 days from close date
            tast task2 = new task(WhatId = lot.id, WhoId = lot.default_case_owner__c,
                                ActivityDate = lot.Close_Date__c.addDays(11), 
                                Status = 'In Progress', 
                                Subject = 'Call');
            tasksTobeInserted.add(task2);
        }
    }
    if(!tasksTobeInserted.isEmpty()) insert tasksTobeInserted;
}