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
Scotty ForceScotty Force 

Trigger question regarding RecordTypeName

I've written a basic trigger, which has recordtypeid as hard coded value, need help switching criteria to recordtype.name. So i can clean up the code. Please help. Thanks!!


trigger AccountCurrencyTrigger on Account (before insert,before update) 
{
   
    for(account acc:trigger.new)
    {
        if(acc.Currency_Test__c < 0 && acc.RecordTypeID == '018f4567348MBvb')
        {
            System.debug('Record Type ID: '+acc.RecordTypeID);
            
            acc.adderror('Sorry, Currency cannot have negative value');
            
        }  
    }
}
Best Answer chosen by Scotty Force
Manj_SFDCManj_SFDC
RecordType rt = [SELECT Id FROM RecordType WHERE DeveloperName = 'RecordTypeAPIName'];
In your trigger you can filter on your records by simply Where RecordTypeId = rt.Id;

All Answers

Raj VakatiRaj Vakati
You can try as shown below 

 
trigger AccountCurrencyTrigger on Account (before insert,before update) 
{

   
    for(account acc:trigger.new)
    {
        if(acc.Currency_Test__c < 0 && acc.RecordType.Name == 'Sales')
        {
            System.debug('Record Type ID: '+acc.RecordType.Name);
            
            acc.adderror('Sorry, Currency cannot have negative value');
            
        }  
    }
}

you can get record type id as shown below also 

Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId(); 
Manj_SFDCManj_SFDC
RecordType rt = [SELECT Id FROM RecordType WHERE DeveloperName = 'RecordTypeAPIName'];
In your trigger you can filter on your records by simply Where RecordTypeId = rt.Id;
This was selected as the best answer
Scotty ForceScotty Force
Thank You, the solution works!!
Manj_SFDCManj_SFDC
Good to know it helped you , please mark the question as solved