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
Ryan CoeRyan Coe 

Apex Trigger Removal or Modification

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: 
AshlekhAshlekh
Hi,

Use below 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= new List<Lead>();
    For (Lead l:[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE])
	{
        l.Status=desiredNewLeadStatus;
		leadsToUpdate.add(l);
    }//for
   
    try{
		if(leadsToUpdate != null && leadsToUpdate.size()>0)
        update leadsToUpdate;
    }catch(DMLException e){
        system.debug('Leads were not all properly updated.  Error: '+e);
    }
}//trigger


Gigi.OchoaGigi.Ochoa
You are most likely getting that error because this trigger will run anytime a Task is inserted or updated.  If you are trying to insert a Task without a WhoId (i.e. an Account without a Contact), you will get an error.

While you are looping through your tasks, make sure the WhoId is not null and is a Lead.
if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE){

Also, before you insert leadsToUpdate, verify that there are leads in that list.  If you try to insert an empty list, you will also get an error.
if(leadsToUpdate != null && leadsToUpdate.size() > 0) insert leadsToUpdate;





Ryan CoeRyan Coe
Thank you D-Horse! I’m not sure I know how to change the code. I went into Apex Triggers -> Developer Console and then found then opened the Lead Status Change entity and copy and pasted your code and then when I save it gives me the error ‘Cant Alter Metadata in Active Org’. I’m sorry, I don’t have the greatest background in this. Any more help would be appreciated, thanks again. Ryan Ryan Coe Sales Analyst, CRM ShopperTrak Chicago USA O: 312.529.5307 | rcoe@shoppertrak.com | @shoppertrak www.shoppertrak.com CONFIDENTIALITY AND SECURITY NOTICE The contents of this message and any attachments may be privileged, confidential and proprietary and also may be covered by the Electronic Communications Privacy Act. If you are not an intended recipient, please inform the sender of the transmission error and delete this message immediately without reading, disseminating, distributing or copying the contents. ShopperTrak makes no assurances that this e-mail and any attachments are free of viruses and other harmful code. Please contact ShopperTrak at 312-676-8222 or email information@shoppertrak.com if you need assistance.
Gigi.OchoaGigi.Ochoa
sorry I meant update leadsToUpdate... not insert.  :)
Ryan CoeRyan Coe
Hi Gigi-

Thank you for all of your help!

I am in the sandbox now, so I am trying to modify the code.

Per your recommendation, I tried placing this in line 7: if(t.whoId != Lead && String.valueOf(t.whoId).startsWith('00Q')==TRUE){

But I am getting an error: Error: Compile Error: Variable does not exist: Lead at line 7 column 27

Do you know what I am doing wrong here?

Again, thank you for your help.

Thanks,

Ryan

Gigi.OchoaGigi.Ochoa
change
if(t.whoId != Lead && String.valueOf(t.whoId).startsWith('00Q')==TRUE){

to
if(t.whoId != null && String.valueOf(t.whoId).startsWith('00Q')==TRUE){