Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
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
3 answers
  1. Oct 29, 2015, 3:22 PM
    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 
Loading
0/9000