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
rumdumdumrumdumdum 

Restricting trigger to Record Type!

Hey Guys,

 

Looking for a way to restrict a trigger when a specific opportunity record type isn't selected.

 

My Code:

 

 

 

trigger Opportunity on Opportunity (after insert) {

List<OpportunityLineItem> blineItems = new List<OpportunityLineItem>();

 

for(Opportunity o : trigger.new){

string a;

string b = o.Product2__c;

String c = o.RecordTypeId;

 

if(c == '012T00000000LSb') {

if(b == 'VOIP') {a = '01uT0000000zxBPIAY';}

else if(b == 'Radio') {a = '01uT0000000zxC9IAI';}

else {a = '01uT0000000zxC2IAI';}

 

blineItems.add(new OpportunityLineItem(quantity = o.Product_Quantity__c,

UnitPrice = o.Product_Price__c,

OpportunityId = o.Id,

pricebookentryid = a

));

}

}

insert blineItems;}

 

  And maybe there's a more efficient way to use Case for PriceBookEntryId's: VOIP, Radio etc...

 

Thanks in advance! 

 

Best Answer chosen by Admin (Salesforce Developers) 
Greg HGreg H

I'm not entirely sure if you want the trigger to fire when the RecordTypeId is a specific value or to fire when it is not a specific value. If the check is for when the RecordTypeId is a certain value then your code should work fine. If you're checking when not equal to a certain value use "if (c != '012T00000000LSb') {".

The other thing you may want to verify is that you can perform the comparison with the 15 character version of the Id versus the 18 character version. I usually use the full 18 character Id value when performing comparisons.
-greg

All Answers

Greg HGreg H

I'm not entirely sure if you want the trigger to fire when the RecordTypeId is a specific value or to fire when it is not a specific value. If the check is for when the RecordTypeId is a certain value then your code should work fine. If you're checking when not equal to a certain value use "if (c != '012T00000000LSb') {".

The other thing you may want to verify is that you can perform the comparison with the 15 character version of the Id versus the 18 character version. I usually use the full 18 character Id value when performing comparisons.
-greg

This was selected as the best answer
rumdumdumrumdumdum
Thanks Greg, no doubt the issue was with the character count. I've got another solution in place, but will definitely check this out!