Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a requirement where I have to write some logic in an Opportunity Trigger.

Requiremnt is , I have find out current opportunity's record type and have to check whether that recordtype name contains some value or not.

Is there any way to find out the current opportunity record's Recordtype Name ?

I found 1 useful link: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_Schema_RecordTypeInfo.htm

But, confused how to use that in my logic.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

If you are doing this in the trigger it would be something like this:

trigger OnOpportunity on Opportunity(before update){

     Map<ID,RecordType> rt_Map = New Map<ID,RecordType>([Select ID, Name From RecordType Where sObjectType = 'Opportunity']);

     for(Opportunity opp : trigger.new){

          if(rt_map.get(opp.recordTypeID).name.containsIgnoreCase('YOUR VALUE')){
               //Do your stuff
          }

     }


}
share|improve this answer

If you have a deep stack of dependent triggers (and some Orgs do) you can save yourself a SOQL by adding a formula field to the opportunity object, with a return type of Text, and a value of

RecordType.Name

If for example the formula field is called RecordTypeName you can access it directly in the trigger without querying again.

for(Opportunity opp : trigger.new){
    if(opp.RecordTypeName.containsIgnoreCase('YOUR VALUE')){
        //Do your stuff
    }
}

If you don't want to go as far as adding a formula to the object, you can still save the SOQL by using the RecordTypeInfos instead of a SOQL query to get your map:

map<Id, Schema.RecordTypeInfo> rt_map = Schema.getGlobalDescribe()get('Opportunity').getDescribe().getRecordTypeInfosById()
for(Opportunity opp : trigger.new){
    if(rt_map.get(opp.recordTypeID).getName().containsIgnoreCase('YOUR VALUE')){
        //Do your stuff
    }
 }

See https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_Schema_RecordTypeInfo.htm

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.