trigger OpptyLineItem on OpportunityLineItem (after insert, after update) {
    if(Trigger.isAfter) {
        if(Trigger.isInsert && Trigger.isUpdate) { 

            for(OpportunityLineItem o : Trigger.new) {

                try {
                    Contract__c listCont = [SELECT id, LastModifiedById, CreatedDate FROM Contract__c WHERE Opportunity__c = :o.OpportunityId LIMIT 1];

                    Task todo = new Task();
                    todo.RecordtypeId = '0125D000000qwClQAI';
                    todo.OwnerId = listcont.LastModifiedById;                        
                    todo.Subject = 'Sync Contract';
                    todo.Priority = 'Normal';
                    todo.WhatId = listCont.id;
                    todo.Status = 'Not Started';
                    todo.IsReminderSet = True;

                    insert todo;

                } catch(Exception e) {

                try {
                    Proposal__c listProp = [SELECT id, LastModifiedById, CreatedDate FROM Proposal__c WHERE Opportunity__c = :o.OpportunityId ORDER BY CreatedDate DESC LIMIT 1];

                    Task todo2 = new Task();
                    todo2.RecordtypeId = '0125D000000qwClQAI';
                    todo2.OwnerId = listProp.LastModifiedById;
                    todo2.Subject = 'Sync Proposal';
                    todo2.Priority = 'Normal';
                    todo2.WhatId = listProp.id;
                    todo2.Status = 'Not Started';
                    todo2.IsReminderSet = True;

                    insert todo2;

                } catch(Exception e) {




I want to create a task whenever I add or modify an opportunity product. That's why I'm using a trigger, but when an opportunity product is added or modified, a task is not created. Is there a problem with my code?

  • 1
    You should remove those try/catch blocks. Right now, the only thing they're doing is making it harder for you to see what the issue is. Once you do that, please edit your question to include the full text of any error messages that you're seeing (don't try to paraphrase the errors, it's highly likely you'll leave out important information).
    – Derek F
    Jul 28, 2021 at 1:20
  • In addition to removing your empty catch blocks (one of the worst things you can possibly do to yourself or your org), you should learn about removing DML from loops.
    – Adrian Larson
    Jul 28, 2021 at 1:22

1 Answer 1

if (Trigger.isInsert && Trigger.isUpdate) {

This is always false. A trigger context can't be both insert and update at the same time.

That's the immediate cause of your trigger doing nothing. But you have a lot of other issues here.

  • You need to bulkify the trigger (remove all DML and SOQL from loops). See Bulk Apex Triggers on Trailhead.
  • You need to remove exception handlers that swallow exceptions. Catching an exception and throwing it into the debug log is usually worse than useless. See this answer for more discussion.
  • You need to remove hard-coded Ids and use schema methods to dynamically obtain the Ids.
  • You should seriously consider adopting a trigger handler pattern and removing all of this logic from the trigger body.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .