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
Nathan Prats 22Nathan Prats 22 

Apex Trigger - Validation Rule

Hi, 

We have 2 Account Record Types, Standard & Partner. I'm trying to create a simple Apex Trigger to prevent a sales rep to change the account record type from Standard to Partner if the account has related opportunities. 

I wrote this, but as I'm new to Apex I'm pretty sure it's plenty wrong. 

trigger PreventEnablePartner on Account (before update) {

For (Account acc : [SELECT Id,RecordTypeId
                    FROM Account
                    WHERE Id IN (SELECT AccountId FROM Opportunity)
                    AND RecordType.Name = 'Partner']) {
                        
Account oldAccount = Trigger.oldMap.get(acc.ID);

IF(acc.RecordTypeId != oldAccount.RecordTypeId ) {
    Trigger.New[0].addError('Cannot "Enable As Partner" an account with related opportunities.');}
    Else {Return;}
}
}

Do you have any idea ? 

Thanks,
Best Answer chosen by Nathan Prats 22
Gokula KrishnanGokula Krishnan
Hi Nathan,

Try this,
 
trigger PreventEnablePartner on Account (before update) {

	Id PartnerRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Partner').getRecordTypeId();
	Map<id,Account> oldMap = Trigger.oldMap;
	Map<Id, Account> accountsMap = new Map<Id, Account>([Select id, (SELECT Id FROM Opportunities) from Account where id IN :Trigger.new]);
    
	for (Account a: Trigger.new) {
        Account fromQuery = accountsMap.get(a.Id);
        if(fromQuery.Opportunities.size()>0 && a.recordtypeid != oldMap.get(a.Id).recordtypeid && a.recordtypeid == PartnerRecordTypeId){
		a.addError('Cannot "Enable As Partner" an account with related opportunities.');
		}
    } 
}

Thanks  !!!

If it helps you, please mark is as best answer, so it will be helpful for other developers.

All Answers

Gokula KrishnanGokula Krishnan
Hi Nathan,

Try this,
 
trigger PreventEnablePartner on Account (before update) {

	Id PartnerRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Partner').getRecordTypeId();
	Map<id,Account> oldMap = Trigger.oldMap;
	Map<Id, Account> accountsMap = new Map<Id, Account>([Select id, (SELECT Id FROM Opportunities) from Account where id IN :Trigger.new]);
    
	for (Account a: Trigger.new) {
        Account fromQuery = accountsMap.get(a.Id);
        if(fromQuery.Opportunities.size()>0 && a.recordtypeid != oldMap.get(a.Id).recordtypeid && a.recordtypeid == PartnerRecordTypeId){
		a.addError('Cannot "Enable As Partner" an account with related opportunities.');
		}
    } 
}

Thanks  !!!

If it helps you, please mark is as best answer, so it will be helpful for other developers.
This was selected as the best answer
Nathan Prats 22Nathan Prats 22
Hi Gokula, 

Thank you it works great ! I'll use this to build similar validation rules on accounts. Thanks again. 
Gokula KrishnanGokula Krishnan
Welcome Nathan

Regards,
Gokula Krishnan