Skip to main content
added 207 characters in body
Source Link
Trigger.isInsert trigger context is used to check whether Trigger was fired due to Insert operation, in that case we want to create Task. 
Trigger.isUpdate trigger context is used during Update operation, get Old map from Trigger context check if Description(or any other field) got changed, then create Task

trigger Opportunity_Create_Task on Opportunity (before insert, before Update) {
    Set < Id > opIds = new Set < Id > ();
    List < Task > taskList = new List < Task > ();
    List < Opportunity > Opps = Trigger.new;
    List<Opportunity> taskOps = [Select Id,Description from Opportunity where Id in :opIds];
    //Insert Opertaion
    if(Trigger.isInsert){
       for (Opportunity Opp: Opps) {
            Task t = new Task();
            t.WhatId = opp.Id;
            t.Subject = 'QMI Task';
            taskList.add(t);
        }
    }
    //Update Operation
    if(Trigger.isUpdate){
        Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
         for (Opportunity Opp: Opps) {
             //Check If description got changed
             if(Opp.description != oldOppMap.get(opp.Id).description){
                Task t = new Task();
                t.WhatId = opp.Id;
                t.Subject = 'QMI Task';
                taskList.add(t);
             }
           
        }
    }
    
    insert taskList;
}

}

trigger Opportunity_Create_Task on Opportunity (before insert, before Update) {
    Set < Id > opIds = new Set < Id > ();
    List < Task > taskList = new List < Task > ();
    List < Opportunity > Opps = Trigger.new;
    List<Opportunity> taskOps = [Select Id,Description from Opportunity where Id in :opIds];
    //Insert Opertaion
    if(Trigger.isInsert){
       for (Opportunity Opp: Opps) {
            Task t = new Task();
            t.WhatId = opp.Id;
            t.Subject = 'QMI Task';
            taskList.add(t);
        }
    }
    //Update Operation
    if(Trigger.isUpdate){
        Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
         for (Opportunity Opp: Opps) {
             //Check If description got changed
             if(Opp.description != oldOppMap.get(opp.Id).description){
                Task t = new Task();
                t.WhatId = opp.Id;
                t.Subject = 'QMI Task';
                taskList.add(t);
             }
           
        }
    }
    
    insert taskList;
}
Trigger.isInsert trigger context is used to check whether Trigger was fired due to Insert operation, in that case we want to create Task. 
Trigger.isUpdate trigger context is used during Update operation, get Old map from Trigger context check if Description(or any other field) got changed, then create Task

trigger Opportunity_Create_Task on Opportunity (before insert, before Update) {
Set < Id > opIds = new Set < Id > ();
List < Task > taskList = new List < Task > ();
List < Opportunity > Opps = Trigger.new;
List<Opportunity> taskOps = [Select Id,Description from Opportunity where Id in :opIds];
//Insert Opertaion
if(Trigger.isInsert){
   for (Opportunity Opp: Opps) {
        Task t = new Task();
        t.WhatId = opp.Id;
        t.Subject = 'QMI Task';
        taskList.add(t);
    }
}
//Update Operation
if(Trigger.isUpdate){
    Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
     for (Opportunity Opp: Opps) {
         //Check If description got changed
         if(Opp.description != oldOppMap.get(opp.Id).description){
            Task t = new Task();
            t.WhatId = opp.Id;
            t.Subject = 'QMI Task';
            taskList.add(t);
         }
       
    }
}

insert taskList;

}

added 120 characters in body
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431
trigger Opportunity_Create_Task on Opportunity (before insert, before Update) {
    Set < Id > opIds = new Set < Id > ();
    List < Task > taskList = new List < Task > ();
    List < Opportunity > Opps = Trigger.new;
    List<Opportunity> taskOps = [Select Id,Description from Opportunity where Id in :opIds];
    //Insert Opertaion
    if(Trigger.isInsert){
       for (Opportunity Opp: Opps) {
            Task t = new Task();
            t.WhatId = opp.Id;
            t.Subject = 'QMI Task';
            taskList.add(t);
        }
    }
    //Update Operation
    if(Trigger.isUpdate){
        Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
         for (Opportunity Opp: Opps) {
             //Check If description got changed
             if(Opp.description != oldOppMap.get(opp.Id).description){
                Task t = new Task();
                t.WhatId = opp.Id;
                t.Subject = 'QMI Task';
                taskList.add(t);
             }
           
        }
    }
    
    insert taskList;
}

}

trigger Opportunity_Create_Task on Opportunity (before insert, before Update) {
Set < Id > opIds = new Set < Id > ();
List < Task > taskList = new List < Task > ();
List < Opportunity > Opps = Trigger.new;
List<Opportunity> taskOps = [Select Id,Description from Opportunity where Id in :opIds];
//Insert Opertaion
if(Trigger.isInsert){
   for (Opportunity Opp: Opps) {
        Task t = new Task();
        t.WhatId = opp.Id;
        t.Subject = 'QMI Task';
        taskList.add(t);
    }
}
//Update Operation
if(Trigger.isUpdate){
    Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
     for (Opportunity Opp: Opps) {
         //Check If description got changed
         if(Opp.description != oldOppMap.get(opp.Id).description){
            Task t = new Task();
            t.WhatId = opp.Id;
            t.Subject = 'QMI Task';
            taskList.add(t);
         }
       
    }
}

insert taskList;

}

trigger Opportunity_Create_Task on Opportunity (before insert, before Update) {
    Set < Id > opIds = new Set < Id > ();
    List < Task > taskList = new List < Task > ();
    List < Opportunity > Opps = Trigger.new;
    List<Opportunity> taskOps = [Select Id,Description from Opportunity where Id in :opIds];
    //Insert Opertaion
    if(Trigger.isInsert){
       for (Opportunity Opp: Opps) {
            Task t = new Task();
            t.WhatId = opp.Id;
            t.Subject = 'QMI Task';
            taskList.add(t);
        }
    }
    //Update Operation
    if(Trigger.isUpdate){
        Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
         for (Opportunity Opp: Opps) {
             //Check If description got changed
             if(Opp.description != oldOppMap.get(opp.Id).description){
                Task t = new Task();
                t.WhatId = opp.Id;
                t.Subject = 'QMI Task';
                taskList.add(t);
             }
           
        }
    }
    
    insert taskList;
}
Source Link

trigger Opportunity_Create_Task on Opportunity (before insert, before Update) {
Set < Id > opIds = new Set < Id > ();
List < Task > taskList = new List < Task > ();
List < Opportunity > Opps = Trigger.new;
List<Opportunity> taskOps = [Select Id,Description from Opportunity where Id in :opIds];
//Insert Opertaion
if(Trigger.isInsert){
   for (Opportunity Opp: Opps) {
        Task t = new Task();
        t.WhatId = opp.Id;
        t.Subject = 'QMI Task';
        taskList.add(t);
    }
}
//Update Operation
if(Trigger.isUpdate){
    Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
     for (Opportunity Opp: Opps) {
         //Check If description got changed
         if(Opp.description != oldOppMap.get(opp.Id).description){
            Task t = new Task();
            t.WhatId = opp.Id;
            t.Subject = 'QMI Task';
            taskList.add(t);
         }
       
    }
}

insert taskList;

}