• Ryan Coe
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hello-

Yesterday, I installed an apex trigger titled 'Change Lead Status' with the intent that anytime a Lead has a completed activity logged, it changes the Lead Status to 'Working - Contact Made'. While that functionality works just fine, my users have noticed an error message whenever they try and log an activity (manually) on an Account without a Contact included on the Name field in the Task page layout. The error will not let them log the Task without a Contact associated with it. I don't understand what is happening, and I'm confused as to why a Lead based apex trigger is affecting Accounts/Contacts.

I have two questions A) is there any way to modify this code so that this data is not required to log a Task manually? The apex trigger works fine other than this error. B) If not, how would I go about removing this apex trigger? I looked at some documentation around this, and they direct me to the sandbox to remove the trigger, but the trigger does not appear in my sandbox. 

Here is the code: 

trigger changeLeadStatus on Task (before insert, before update) {
    String desiredNewLeadStatus = 'Working - Contacted';

    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new){
        if(t.Status=='Completed'){
            if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
                leadIds.add(t.whoId);
            }//if 2
        }//if 1
    }//for
    List<Lead> leadsToUpdate=[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE];
    For (Lead l:leadsToUpdate){
        l.Status=desiredNewLeadStatus;
    }//for
   
    try{
        update leadsToUpdate;
    }catch(DMLException e){
        system.debug('Leads were not all properly updated.  Error: '+e);
    }
}//trigger

Any help would be greatly appreciated, I have received several complaints over the past day. 

Thank you,

Ryan

The code is as follows: 
Hello-

Yesterday, I installed an apex trigger titled 'Change Lead Status' with the intent that anytime a Lead has a completed activity logged, it changes the Lead Status to 'Working - Contact Made'. While that functionality works just fine, my users have noticed an error message whenever they try and log an activity (manually) on an Account without a Contact included on the Name field in the Task page layout. The error will not let them log the Task without a Contact associated with it. I don't understand what is happening, and I'm confused as to why a Lead based apex trigger is affecting Accounts/Contacts.

I have two questions A) is there any way to modify this code so that this data is not required to log a Task manually? The apex trigger works fine other than this error. B) If not, how would I go about removing this apex trigger? I looked at some documentation around this, and they direct me to the sandbox to remove the trigger, but the trigger does not appear in my sandbox. 

Here is the code: 

trigger changeLeadStatus on Task (before insert, before update) {
    String desiredNewLeadStatus = 'Working - Contacted';

    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new){
        if(t.Status=='Completed'){
            if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
                leadIds.add(t.whoId);
            }//if 2
        }//if 1
    }//for
    List<Lead> leadsToUpdate=[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE];
    For (Lead l:leadsToUpdate){
        l.Status=desiredNewLeadStatus;
    }//for
   
    try{
        update leadsToUpdate;
    }catch(DMLException e){
        system.debug('Leads were not all properly updated.  Error: '+e);
    }
}//trigger

Any help would be greatly appreciated, I have received several complaints over the past day. 

Thank you,

Ryan

The code is as follows: