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 

Help with running Trigger on a specific Record Type

Hello, 

I need the trigger below to only run for a specific opportunity record type called 'Conference'. Any assistance is greatly appreciated.
 
trigger UpdateOpportunityProduct on Opportunity (before insert, before update ){

    Map<String, Opportunity> discCodeToOppMap = new Map<String, Opportunity>();

    for(Opportunity opp:Trigger.new){

    discCodeToOppMap.put(opp.Discount_Code__c, opp);

    }

    List<Product2> prodLst = [SELECT ID, ProductCode FROM Product2 where ProductCode in :discCodeToOppMap.keySet()];

    for(Product2 prod : prodLst){
    
        discCodeToOppMap.get(prod.ProductCode).Discount_Code_LU__c = prod.ID;
    }
}

 
Abhishek BansalAbhishek Bansal
Hi,

Please fetch the record type and compare it with the opportunity record. I have updated the code below:
trigger UpdateOpportunityProduct on Opportunity (before insert, before update ){
    
	Id CONFERENCER_RECORD_TYPE_ID = SObjectType.Opportunity.getRecordTypeInfosByName().get('Conference').getRecordTypeId();
    Map<String, Opportunity> discCodeToOppMap = new Map<String, Opportunity>();

    for(Opportunity opp:Trigger.new){
        if(opp.RecordTypeId == CONFERENCER_RECORD_TYPE_ID) {
            discCodeToOppMap.put(opp.Discount_Code__c, opp);
		}

    }

    List<Product2> prodLst = [SELECT ID, ProductCode FROM Product2 where ProductCode in :discCodeToOppMap.keySet()];

    for(Product2 prod : prodLst){
        discCodeToOppMap.get(prod.ProductCode).Discount_Code_LU__c = prod.ID;
    }
}
//Please take care of any syntax error
Thanks,
Abhishek Bansal.