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
AwkwardAAwkwardA 

Trigger only working before update, not on before insert

I've created the following trigger and it is only updating the task before an update and not before an insert. Help me troubleshoot?

trigger EmailISResponse on Task (before insert, before update) {

List<User> InsideSalesUser = [SELECT Id FROM User where UserRoleId='00E30000001rctV'];

Set<Id> InsideSalesUserId = new set<Id>();
if (InsideSalesUser != NULL){

for (User u :InsideSalesUser) {
    InsideSalesUserId.add(u.Id);
    }
}

List<Lead> LeadsWithCampaign = [SELECT Id FROM Lead where Current_Teleprospecting_Campaign__c != NULL];
Set<Id> LeadsWithCampaignId = new set<Id>();
if (LeadsWithCampaign != NULL){

for (Lead l :LeadsWithCampaign) {
    LeadsWithCampaignId.add(l.id);
    }
}

for (Task t: Trigger.new) {
    if(InsideSalesUserId.contains(t.OwnerID) && t.WhoId != NULL && LeadsWithCampaignId.contains(t.WhoId) && t.IsClosed == true && String.valueOf(t.WhoId).startsWith('00Q')&& t.Subject != NULL && String.valueOf(t.Subject).startsWith('Email'))
    
    t.IS_Response__c = true;
    }
}


Jen BennettJen Bennett
After you create a new record for testing, did you query the fields in your condition above to make sure they all meet the criteria? 
mrtelesmrteles
Hi AwkwardA,

Try this solution,

for (Task t: Trigger.new) {
    if(InsideSalesUserId.contains(t.OwnerID) && t.WhoId != NULL && LeadsWithCampaignId.contains(t.WhoId) && t.IsClosed == true && String.valueOf(t.WhoId).startsWith('00Q')&& t.Subject != NULL && String.valueOf(t.Subject).startsWith('Email'))
    
    t.IS_Response__c = true;
    }
}


//switch your code and try to this above
if(Trigger.isBefore) {
      if(Trigger.isUpdate || Trigger.isInsert) {
        for (Task t: Trigger.new) {
            if(InsideSalesUserId.contains(t.OwnerID) && t.WhoId != NULL && LeadsWithCampaignId.contains(t.WhoId) && t.IsClosed == true && String.valueOf(t.WhoId).startsWith('00Q')&& t.Subject != NULL && String.valueOf(t.Subject).startsWith('Email'))
            
            t.IS_Response__c = true;
            }
        }
}
Let me know if this solved you problem.
AwkwardAAwkwardA
Yes, the task and lead met all the conditions in the criteria.

mrteles, tried your solution but it's still not working on insert.
Jen BennettJen Bennett
Maybe do a system debug on the ownerid, I am wondering if that is set before it's inserted or not. You could try removing that part of the condition and see if it works at that point or not. Also thinking you could eliminate the startsWith('00Q') it isn't causing any issues but should be covered in the leadswithcampaignId.contains condition.
DazedPuzzledDazedPuzzled
Is it going to line # 25?Have you debugged the If condition at row #23 ?
mrtelesmrteles
Sorry, try again: 
if(Trigger.isBefore) {
      if(Trigger.isUpdate || Trigger.isInsert) {
        for (Task t: Trigger.new) {
            for(Task ot: Trigger.old) {
                if(o.Id ==ot.Id && InsideSalesUserId.contains(t.OwnerID) && t.WhoId != NULL && LeadsWithCampaignId.contains(t.WhoId) && t.IsClosed == true && String.valueOf(t.WhoId).startsWith('00Q')&& t.Subject != NULL && String.valueOf(t.Subject).startsWith('Email'))
                    t.IS_Response__c = true;
            }
        }	
        }
}

Let me know if this solved you problem.