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
Mike FitchMike Fitch 

How do I create an apex trigger that applies to a certain record type?

I created a trigger that works with the Opportunity object, however I need it to only work for a particular record type. I'm pretty new to apex development so I'm a beginner at best. The trigger works and I managed 75% code coverage, but it's effecting the other record types in my org and causing errors. Any help with this issue would be appreciated. Thanks

Here is my trigger:

trigger ClosedLostOtherInactive on Opportunity (before update) {
   
     for (Opportunity closedlost : Trigger.new) {
         if (closedlost.StageName == 'Closed Lost')
         {
         If ( (closedlost.Closed_Lost_Other__c == false && closedlost.Closed_Lost_Inactive__c == false)) {

             closedlost.adderror('Please select either Closed Lost - Other or Closed Lost - Inactive checkbox ');
         }
         if (closedlost.Lost_Detail_Other_or_Inactive__c == null)
         {
             closedlost.adderror('Lost Detail - Other or Inactive text field is required ');
         }
         }
         If (closedlost.Closed_Lost_Inactive__c == True) {
                      
             closedlost.Closed_Lost_Other__c = false ;
         }
     }
}


And here is the test class:

@isTest
public class TestClosedLostOtherInactive{
   
static testmethod void insertOpportunity() {

        Opportunity o = new Opportunity();
  
    o.Name = 'TestOpp';
    o.CloseDate = System.today();
    o.StageName = 'Identifying';
    o.CurrencyIsoCode = 'USD';
    o.Closed_Lost_Inactive__c = False;
    o.Closed_Lost_Other__c = False;
    o.Lost_Detail_Other_or_Inactive__c = null;
  
    insert o;
   
      o.StageName = 'Closed Lost';
        o.Closed_Lost_Inactive__c = True;
        o.Closed_Lost_Other__c = False;
        o.Lost_Detail_Other_or_Inactive__c = 'Test';
        update o;
    }
       
}


Best Answer chosen by Mike Fitch
Ramu_SFDCRamu_SFDC
Just add one additional line as suggested in bold text. Replace <recordtypeid> with the id of the record type you require. To get the record type id, logon to your salesforce org, go to setup-->standardobject-->recordtypes--> click on the record type you want the id for and copy the id from the url. 

trigger ClosedLostOtherInactive on Opportunity (before update) {
  
     for (Opportunity closedlost : Trigger.new) {
if(closedlost.recordtypeid=='<recordtypeid>'){
         if (closedlost.StageName == 'Closed Lost')
         {
         If ( (closedlost.Closed_Lost_Other__c == false && closedlost.Closed_Lost_Inactive__c == false)) {

             closedlost.adderror('Please select either Closed Lost - Other or Closed Lost - Inactive checkbox ');
         }
         if (closedlost.Lost_Detail_Other_or_Inactive__c == null)
         {
             closedlost.adderror('Lost Detail - Other or Inactive text field is required ');
         }
         }
         If (closedlost.Closed_Lost_Inactive__c == True) {
                     
             closedlost.Closed_Lost_Other__c = false ;
         }
     }
}
}

All Answers

Ramu_SFDCRamu_SFDC
Just add one additional line as suggested in bold text. Replace <recordtypeid> with the id of the record type you require. To get the record type id, logon to your salesforce org, go to setup-->standardobject-->recordtypes--> click on the record type you want the id for and copy the id from the url. 

trigger ClosedLostOtherInactive on Opportunity (before update) {
  
     for (Opportunity closedlost : Trigger.new) {
if(closedlost.recordtypeid=='<recordtypeid>'){
         if (closedlost.StageName == 'Closed Lost')
         {
         If ( (closedlost.Closed_Lost_Other__c == false && closedlost.Closed_Lost_Inactive__c == false)) {

             closedlost.adderror('Please select either Closed Lost - Other or Closed Lost - Inactive checkbox ');
         }
         if (closedlost.Lost_Detail_Other_or_Inactive__c == null)
         {
             closedlost.adderror('Lost Detail - Other or Inactive text field is required ');
         }
         }
         If (closedlost.Closed_Lost_Inactive__c == True) {
                     
             closedlost.Closed_Lost_Other__c = false ;
         }
     }
}
}
This was selected as the best answer
Mike FitchMike Fitch
That did the trick!! Thanks so much for your help.
Mike FitchMike Fitch
SOQL Queries are a little beyond my skillset. I just wanted it functional at this point. There are only two record types in my org. Do you see this being a problem in the future if I don't use your suggestion?
Mike FitchMike Fitch
I copied the recordtype id from production and it matches the id in the sandbox. I'm not understanding why they would be different. As I said earlier I am very new to apex development so please bear with me. I made the adjustments to the test class to get coverage and it passed. It appears i can move it directly to production. Why would the trigger fail?
Mike FitchMike Fitch
Ok the article explained it. I understand now. I appreciate the link and for all of your help.