6

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.

17

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

trigger OnOpportunity on Opportunity(before update){

     Map<ID,Schema.RecordTypeInfo> rt_Map = Opportunity.sObjectType.getDescribe().getRecordTypeInfosById();

     for(Opportunity opp : trigger.new){

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

     }


}
  • Your code works pretty well.But like suppose is it works like in one record type some custom picklist is there.Whenever user selects value in that picklist. it's automatically saved to standard object field @Eric – Pavan tej Jan 29 '16 at 17:17
7

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

  • With describe limits removed this option is my preference – Phil B Oct 22 '14 at 15:21
  • I think it should be opp.RecordType.Name.containsIgnoreCase('YOUR VALUE'). Note the dot operator after RecordType. – Ruwantha Oct 21 '16 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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