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
Edward EastEdward East 

Make a record type specific Lead Status update trigger

Hi all,

I am trying to implement a tasks apex trigger that would automatically change a Lead's status from open to contacted when an Email has been sent out.
I have used the below code with success in the past, but since then we have created three unique record types, one of which has different Statuses and should never be afftected by this trigger. How do I specify that this trigger should only fire for the remaining two record types?

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

    /*String desiredNewLeadStatus = 'Contacted';
    String task_subject_check = 'Email: Would you like to do more Brand Endorsements?';
    List<Id> leadIds=new List<Id>();
    
    for(Task t:trigger.new){
        if(t.Status=='Completed'){
            if(t.whoId!=NULL){//check if the task is associated with a lead
                if(t.Subject.contains(task_subject_check)){
                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
Best Answer chosen by Edward East
AshlekhAshlekh
Hi,

You can put the condition of RecordType.name = '"Name of record Type",.

If you have created record type on Lead then you can query the only that record type on which you want to update.

Like this : SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE and ( RecordType.name = 'REcord type name 1' or RecordType.name = 'REcord type name 2')

Or if you have create record type on Task than collect leadIds only that tasks which having that record type.

 if(t.Subject.contains(task_subject_check) and ( t.RecordTypeId = '1 RT' or t.RecordTypeId = '2 RT' ) ){
                leadIds.add(t.whoId);}
            }//if 2

-thanks
Ashlekh Gera 

All Answers

AshlekhAshlekh
Hi,

You can put the condition of RecordType.name = '"Name of record Type",.

If you have created record type on Lead then you can query the only that record type on which you want to update.

Like this : SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE and ( RecordType.name = 'REcord type name 1' or RecordType.name = 'REcord type name 2')

Or if you have create record type on Task than collect leadIds only that tasks which having that record type.

 if(t.Subject.contains(task_subject_check) and ( t.RecordTypeId = '1 RT' or t.RecordTypeId = '2 RT' ) ){
                leadIds.add(t.whoId);}
            }//if 2

-thanks
Ashlekh Gera 
This was selected as the best answer
Edward EastEdward East
Thanks, I got it to work using this trigger:

trigger changeLeadStatus on Task (before insert, before update) {
    String desiredNewLeadStatus = 'Contacted';
    String tasksubjectcheck = 'Email';
    List<Id> leadIds=new List<Id>();
    
    for(Task t:trigger.new){
        if(t.Status=='Completed'){
            if(t.whoId!=NULL){//check if the task is associated with a lead
                if(t.Subject.contains(tasksubjectcheck)) {
                    leadIds.add(t.whoId);
                    }
            }//if 2
        }//if 1
    }//for
    List<Lead> leadsToUpdate=[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE AND ( RecordType.name = 'RecordType1' or RecordType.name = 'RecordType2')];
    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

However, when I want to deploy it in production it says that the trigger has 0% code coverage. This is my test class:

@isTest
private class testChangeLeadStatus {
    static testMethod void testChangeLeadStatusTrigger(){
       /* String desiredNewLeadStatus='Contacted';
        
        Lead l= new Lead (LastName='test', Company='company');
        insert l;
        Task t=new Task (whoId=l.Id, subject='Test task', Status='Completed');

        Test.StartTest();
            insert t;
        Test.StopTest();
        
        Lead l2=[Select Id, Status FROM Lead WHERE Id=:l.Id];
        system.assertEquals(desiredNewLeadstatus,l2.Status);*/
    }
}

Any idea why this is happening?
Edward EastEdward East
Figured it out, thanks for the help though!