You need to sign in to do that
Don't have an account?
Mike 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;
}
}
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;
}
}
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
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 ;
}
}
}
}