You need to sign in to do that
Don't have an account?
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
@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 (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:
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:
Regards
Andrew
All Answers
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();
}
}
@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 (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:
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:
Regards
Andrew
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.