You need to sign in to do that
Don't have an account?
Nathan 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,
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,
Try this,
Thanks !!!
If it helps you, please mark is as best answer, so it will be helpful for other developers.
All Answers
Try this,
Thanks !!!
If it helps you, please mark is as best answer, so it will be helpful for other developers.
Thank you it works great ! I'll use this to build similar validation rules on accounts. Thanks again.
Regards,
Gokula Krishnan