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
CRMsimpleCRMsimple 

Validate that a record is a certain record type before executing trigger

I have a trigger that is creating child records when a parent record is created and populates fields on the chiuld record with values from the parent. This is working fine but now I need to make sure the parent record is a certain record type or the trigger should not create the child. This is my very first trigger so I don't have tons of experience to really take it any further.

 

Here's the code I have:

 

 

trigger AutoCreatePartnerCommission on Opportunity (after insert) {List<Commission__c> commissions = new List<Commission__c> () ;//For each opportunity processed by the trigger, add a new//commission record for the specified partner account.//Note that the Trigger.New is a list of all the new opportunities//that are being created.for (Opportunity newOpportunity: Trigger.New) {IF (newOpportunity.Days_Since_Submitted__c >= 0 ) { commissions.add (new Commission__c( Account__c = newOpportunity.Partner_Account__c, Original_Opportunity_Amt__c = newOpportunity.Amount, Opportunity__c = newOpportunity.id, Loan_Amount__c = newOpportunity.Maximum_Loan_Amount__c )); }}insert commissions;}

 Where/how do I get the record type criteria in there? Any help would be appreciated.

 

Jeremy King 

 

 

jrotensteinjrotenstein

The RecordTypeId is on the Opportunity. However, instead of referring to it directly, you should first retrieve the desired value. This lets you run the Trigger in other instances (eg Dev, Test).

You'd want something like this:

 

trigger AutoCreatePartnerCommission on Opportunity (after insert) {

List<Commission__c> commissions = new List<Commission__c> () ;

String desiredRecordType = [select Id from RecordType where Name = 'Commissionable'].Id;

//For each opportunity processed by the trigger, add a new
//commission record for the specified partner account.
//Note that the Trigger.New is a list of all the new opportunities
//that are being created.
for (Opportunity newOpportunity: Trigger.New) {
IF (newOpportunity.Days_Since_Submitted__c >= 0 && newOpportunity.RecordTypeId = desiredRecordType) {
commissions.add (new Commission__c(
Account__c = newOpportunity.Partner_Account__c,
Original_Opportunity_Amt__c = newOpportunity.Amount,
Opportunity__c = newOpportunity.id,
Loan_Amount__c = newOpportunity.Maximum_Loan_Amount__c ));
}
}
insert commissions;
}