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
Ron08Ron08 

query account record type from opportunity

I have a trigger on Opps and need to execute some logic if the opp is associated to only one Account Record type. How do I do this?

trigger UpdateTeamMember on Opportunity (After insert, after update) {
    if(trigger.isUpdate){
        for(opportunity o : trigger.new){
            if(o.account.recordtypeid == 'xxxxx') >> does not work?

Any ideas?
Best Answer chosen by Ron08
YogeshMoreYogeshMore
Hi Ron08,

There is multiple ways to get record Type Id. By using schema class, you can achieve this.
String accRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('record type name').getRecordTypeId();

For your scenario you can use above code either, you can achieve by following way –
trigger UpdateTeamMember on Opportunity (After insert, after update) {
    
    if(trigger.isUpdate){
        List<String> lstOfAccountId = new   List<String>();
        
        for(opportunity o : trigger.new){
            lstOfAccountId.add(o.accountId);
        }
        
        Map<Id, Account> mapOfAccount = new Map<Id, Account>([select id, Name, recordTypeId from Account where Id IN : lstOfAccountId ]);
        
        for(opportunity o : trigger.new){
            if(mapOfAccount.containsKey(o.accountId)){
                String accRecordTypeId = mapOfAccount.get(o.accountId).recordTypeId;
            }
        }
    }
}
Let us know if it helps you.

Regards,
Yogesh More
more.yogesh422@gmail.com || Skype:-yogesh.more44
 

All Answers

Alex EzhilarasanAlex Ezhilarasan
Create a formula field in opportunity for Account.RecordTypeId. Use that formula field in trigger to check
Ron08Ron08
Yes, I did think about that Alex, but I am trying to learn apex and was stick at this point, so it would be nice to know the code to be able do this?
Thanks
Alex EzhilarasanAlex Ezhilarasan
Hi Ron08,

As of now(without knowing the requirement), I would prefer to using formula field. Because, you need to loop two times and one query to get recordtypeId.

sequence will be:
trigger.new loop 1: Store the Account values.
Query the RecordTypeId for stored Account values in map.
trigger.new loop 2: compare the recordtypeId value to do the rest of operation.

Thanks,
Alex Ezhilarasan

 
YogeshMoreYogeshMore
Hi Ron08,

There is multiple ways to get record Type Id. By using schema class, you can achieve this.
String accRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('record type name').getRecordTypeId();

For your scenario you can use above code either, you can achieve by following way –
trigger UpdateTeamMember on Opportunity (After insert, after update) {
    
    if(trigger.isUpdate){
        List<String> lstOfAccountId = new   List<String>();
        
        for(opportunity o : trigger.new){
            lstOfAccountId.add(o.accountId);
        }
        
        Map<Id, Account> mapOfAccount = new Map<Id, Account>([select id, Name, recordTypeId from Account where Id IN : lstOfAccountId ]);
        
        for(opportunity o : trigger.new){
            if(mapOfAccount.containsKey(o.accountId)){
                String accRecordTypeId = mapOfAccount.get(o.accountId).recordTypeId;
            }
        }
    }
}
Let us know if it helps you.

Regards,
Yogesh More
more.yogesh422@gmail.com || Skype:-yogesh.more44
 
This was selected as the best answer