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
Florence HubbardFlorence Hubbard 

Create a trigger in an opportunity

When a person adds a new note to an opportunity can we have a trigger that comes up once they click save, so they HAVE to create a new task?
Chris  ByromChris Byrom
The only way you can really enforce this would be to create the task for them. If you gather all the data needed on the Opportunity to create the task, you can create the task with a Trigger, Process, etc.
Navee RahulNavee Rahul
Hello florence,

its not possible to to create Task ,when note is attached to a  oppourtunity.

Thanks
D Naveen Rahul.
Chris  ByromChris Byrom
That is not correct Navee. You can create them with a Trigger on the Note object like this. It works fine.
trigger NoteTrigger on Note (before insert) {
    List<Task> tasksToInsert = new List<Task>();
    Task newTask;
    
    for(Note n : trigger.new){
        Schema.SObjectType entityType = n.ParentId.getSobjectType();        
        if(entityType == Opportunity.sObjectType){
            System.debug(entityType);
            //Create Task
            newTask = new Task();
            newTask.WhatId = n.ParentId;
            newTask.Subject = 'New Task';
            tasksToInsert.add(newTask);
        }
        if(tasksToInsert.size() > 0){
            insert tasksToInsert;
        }
    }
}