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
XIOXIO 

Need trigger to only fire for specific opportunity recordtype

Hello,

I need help with the apex trigger below. I need this trigger to only fire for Opportunity RecordTypeId '01250000000HjVU'

Any assistance is greatly appreciated!!
 
trigger AttendeesHandling on Opportunity (after insert,after delete,after update) {

list<Id> rlist1=new list<Id> ();
        list<Product2 > rlsit=new list<Product2>();
        
//////////// insert and update operation
        
 if(trigger.isinsert || trigger.isupdate){
    for(Opportunity o : trigger.new)
   
    {
        
        rlist1.add(o.Discount_code_LU__c);     
    }
 
  Product2 p2=[select id,Attendees__c from Product2 WHERE id in:rlist1];
    
  
  for(Opportunity opp:trigger.new){
   
   p2.Attendees__c=opp.Attendee_Name__c ;
  

   rlsit.add(p2);
    } update rlsit; 
    
  }
   
 /////////////// Delete operation start   
 
    if(trigger.isdelete){
    for(Opportunity o : trigger.old)
   
        
    {
        
        rlist1.add(o.Discount_code_LU__c );     
    }

  Product2 p2=[select id,Attendees__c from Product2 WHERE id in:rlist1 LIMIT 1];
    
  
  for(Opportunity opp:trigger.old){
   
   p2.Attendees__c=opp.Attendee_Name__c ;
 
   rlsit.add(p2);
    } update rlsit; 
    
    }
}

 
Best Answer chosen by XIO
v varaprasadv varaprasad
Hi Harmens

Please check once below code.
In place of Recoedtypelabelname give your recordtype lable name.
 
trigger AttendeesHandling on Opportunity (after insert,after delete,after update) {

list<Id> rlist1=new list<Id> ();
        list<Product2 > rlsit=new list<Product2>();
 Id oppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Recoedtypelabelname').getRecordTypeId();
//////////// insert and update operation
        
 if(trigger.isinsert || trigger.isupdate){
    for(Opportunity o : trigger.new)
   
    {
		if(o.recordtypeid == oppRecordTypeId){
			rlist1.add(o.Discount_code_LU__c);  
		}
    }
 
  Product2 p2=[select id,Attendees__c from Product2 WHERE id in:rlist1];
    
  
  for(Opportunity opp:trigger.new){
   if(o.recordtypeid == oppRecordTypeId){
   p2.Attendees__c=opp.Attendee_Name__c ;
  

   rlsit.add(p2);
   }
    } update rlsit; 
    
  }
   
 /////////////// Delete operation start   
 
    if(trigger.isdelete){
    for(Opportunity o : trigger.old)
    if(o.recordtypeid == oppRecordTypeId){
        
    {
        
        rlist1.add(o.Discount_code_LU__c );     
    }
	}
  Product2 p2=[select id,Attendees__c from Product2 WHERE id in:rlist1 LIMIT 1];
    
  
  for(Opportunity opp:trigger.old){
   
   p2.Attendees__c=opp.Attendee_Name__c ;
 
   rlsit.add(p2);
    } update rlsit; 
    
    }
}
Please check once and let me in case of any help.

Thanks
Varaprasad
 

All Answers

v varaprasadv varaprasad
Hi Harmens

Please check once below code.
In place of Recoedtypelabelname give your recordtype lable name.
 
trigger AttendeesHandling on Opportunity (after insert,after delete,after update) {

list<Id> rlist1=new list<Id> ();
        list<Product2 > rlsit=new list<Product2>();
 Id oppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Recoedtypelabelname').getRecordTypeId();
//////////// insert and update operation
        
 if(trigger.isinsert || trigger.isupdate){
    for(Opportunity o : trigger.new)
   
    {
		if(o.recordtypeid == oppRecordTypeId){
			rlist1.add(o.Discount_code_LU__c);  
		}
    }
 
  Product2 p2=[select id,Attendees__c from Product2 WHERE id in:rlist1];
    
  
  for(Opportunity opp:trigger.new){
   if(o.recordtypeid == oppRecordTypeId){
   p2.Attendees__c=opp.Attendee_Name__c ;
  

   rlsit.add(p2);
   }
    } update rlsit; 
    
  }
   
 /////////////// Delete operation start   
 
    if(trigger.isdelete){
    for(Opportunity o : trigger.old)
    if(o.recordtypeid == oppRecordTypeId){
        
    {
        
        rlist1.add(o.Discount_code_LU__c );     
    }
	}
  Product2 p2=[select id,Attendees__c from Product2 WHERE id in:rlist1 LIMIT 1];
    
  
  for(Opportunity opp:trigger.old){
   
   p2.Attendees__c=opp.Attendee_Name__c ;
 
   rlsit.add(p2);
    } update rlsit; 
    
    }
}
Please check once and let me in case of any help.

Thanks
Varaprasad
 
This was selected as the best answer
XIOXIO
Hi Varaprasad,

Thank you for your reply!

I'm still getting the error below when I try to update the other opportunity recordtypes. The trigger works great for the recordtype 'Conference' but it gives an error for the other recordtypes. I want the trigger to work for the opportunity recordtype 'Conference' ONLY. The trigger shouldn't fire for any other recordtype.

Error:Apex trigger AttendeesHandling caused an unexpected exception, contact your administrator: AttendeesHandling: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: ()
 
trigger AttendeesHandling on Opportunity (after insert,after delete,after update) {

list<Id> rlist1=new list<Id> ();
        list<Product2 > rlsit=new list<Product2>();
 Id oppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Conference').getRecordTypeId();
//////////// insert and update operation
        
 if(trigger.isinsert || trigger.isupdate){
    for(Opportunity o : trigger.new)
   
    {
        if(o.recordtypeid == oppRecordTypeId){
            rlist1.add(o.Discount_code_LU__c);  
        }
    }
 
  Product2 p2=[select id,Attendees__c from Product2 WHERE id in:rlist1];
    
  
  for(Opportunity opp:trigger.new){
   if(opp.recordtypeid == oppRecordTypeId){
   p2.Attendees__c=opp.Attendee_Name__c ;
  

   rlsit.add(p2);
   }
    } update rlsit; 
    
  }
   
 /////////////// Delete operation start   
 
    if(trigger.isdelete){
    for(Opportunity o : trigger.old)
    if(o.recordtypeid == oppRecordTypeId){
        
    {
        
        rlist1.add(o.Discount_code_LU__c );     
    }
    }
  Product2 p2=[select id,Attendees__c from Product2 WHERE id in:rlist1 LIMIT 1];
    
  
  for(Opportunity opp:trigger.old){
   
   p2.Attendees__c=opp.Attendee_Name__c ;
 
   rlsit.add(p2);
    } update rlsit; 
    
    }
}