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
Richard FrayRichard Fray 

Trigger to fire only on certain records

Hi All, 

New to apex and having an issue with a trigger(below). Its firing everytime but i would like it to only fire on records that are of a certain record type.
Any assistance is appreciated.

trigger efs_DealTrigger on Deal__c (after update, after insert) {
    
    List<Deal__c> dealsToUpdate = new list<Deal__c>();
    for (Deal__c deal: trigger.new){
        if(deal.RecordTypeId == '0120Y000000ITgrQAG'){
            dealsToUpdate.add(deal);
        } 
    }
    
    
    for(Deal__c dealsInCriteria: dealsToUpdate) {
        if(trigger.isAfter && trigger.isInsert){
            efs__.EgnyteSyncQueueTrigger.onAfterInsert();
        }
        else if(trigger.isAfter && trigger.isUpdate){
            efs__.EgnyteSyncQueueTrigger.onAfterUpdate();
        }
    }
}

Thanks

Richard

Best Answer chosen by Richard Fray
Andrew GAndrew G
+SRK

@Richard
SRK is correct, The trigger(s) will fire for any DML operation.  We can test some events in the context of the Trigger, basically using the IF statement to build a different collection.
Now in your case, that fact that you are invoking code
efs__.EgnyteSyncQueueTrigger.onAfterInsert();
(which I suspect is the Trigger Handler for the Object) without passing any variable, would lead me to believe that the Trigger Handler is using the Trigger.New / Trigger.Old / etc attributes of the trigger in the Handler to do the processing. This would be evident if in the Trigger Handler you saw something like:
public class EgnyteSyncQueueTrigger{
 public void onAfterInsert(){
    for (Deal__c deal : Trigger.New) {
       // or any reference to Trigger actually 
      ///some processing
    }
  }
}

So in this case, you would not be able to have the trigger only handle records based on RecordType unless you re-write the Trigger Handler.  You would either need to re-write the Handler so that it accepts a list of records, or you will need to re-write some of it's methods so that once invoked it will handle the Trigger attributes to build it's own collection of records based on record type.

As an aside, I don't like hard coded Ids in my code, I would get the RecordType Id using something like:
//get the ID for the RecordType using the name - hard coding Ids in Apex is not best practice
    Id dealRecordTypeId = Schema.SObjectType.Deal__c.getRecordTypeInfosByName().get('MyRecordTypeName').getRecordTypeId();



Regards
Andrew

All Answers

SRKSRK
Triggers will fire everytime you do the DML opration, you cannot bypass that 
you cannot write a trigger spacfic to record type

one issues i can see in this trigger thow is that it calling funcation from for loop, now if there is any SOQL in those fucation than it is same as firing SOQL in a loop which is not a good idea 

you are alreadying collecting the record in a set (i.e. dealsToUpdate) try to pass the whole set at once as a argument in you method and process that set in you method to get the desired result

 for(Deal__c dealsInCriteria: dealsToUpdate) {
        if(trigger.isAfter && trigger.isInsert){
            efs__.EgnyteSyncQueueTrigger.onAfterInsert();
        }
        else if(trigger.isAfter && trigger.isUpdate){
            efs__.EgnyteSyncQueueTrigger.onAfterUpdate();
        }
    }

 
Andrew GAndrew G
+SRK

@Richard
SRK is correct, The trigger(s) will fire for any DML operation.  We can test some events in the context of the Trigger, basically using the IF statement to build a different collection.
Now in your case, that fact that you are invoking code
efs__.EgnyteSyncQueueTrigger.onAfterInsert();
(which I suspect is the Trigger Handler for the Object) without passing any variable, would lead me to believe that the Trigger Handler is using the Trigger.New / Trigger.Old / etc attributes of the trigger in the Handler to do the processing. This would be evident if in the Trigger Handler you saw something like:
public class EgnyteSyncQueueTrigger{
 public void onAfterInsert(){
    for (Deal__c deal : Trigger.New) {
       // or any reference to Trigger actually 
      ///some processing
    }
  }
}

So in this case, you would not be able to have the trigger only handle records based on RecordType unless you re-write the Trigger Handler.  You would either need to re-write the Handler so that it accepts a list of records, or you will need to re-write some of it's methods so that once invoked it will handle the Trigger attributes to build it's own collection of records based on record type.

As an aside, I don't like hard coded Ids in my code, I would get the RecordType Id using something like:
//get the ID for the RecordType using the name - hard coding Ids in Apex is not best practice
    Id dealRecordTypeId = Schema.SObjectType.Deal__c.getRecordTypeInfosByName().get('MyRecordTypeName').getRecordTypeId();



Regards
Andrew
This was selected as the best answer
Richard FrayRichard Fray

Unfortunately the trigger handler in this case is part of a managed package so is (hidden), so edits cannot be made to it. 

It would seem I might be out of luck then and have to think of another solution to this. 

Thank you both for your help and good advice @Andrew, I will avoid using hard coded Id's in future.