You need to sign in to do that
Don't have an account?
I want to create a trigger which can restrict creating opportunity when related account industry is defined
Here we go ...
trigger o1 on Opportunity (Before Insert) {
for(Opportunity o:Trigger.new){
ID aid = o.Account.ID;
Account a = [Select Name,Industry From Account WHERE ID = :aid];
if(a.Industry == 'Education'){
o.addError('No more Opportunity from Education Domain');
}
}
}
Above triggere is not allowing me to create opportunity for any account.
trigger o1 on Opportunity (Before Insert) {
for(Opportunity o:Trigger.new){
ID aid = o.Account.ID;
Account a = [Select Name,Industry From Account WHERE ID = :aid];
if(a.Industry == 'Education'){
o.addError('No more Opportunity from Education Domain');
}
}
}
Above triggere is not allowing me to create opportunity for any account.
Please check the below code:
Regards,
Mahesh
All Answers
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AmitKhanduri602.o1 caused an unexpected exception, contact your administrator: AmitKhanduri602.o1: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.AmitKhanduri602.o1: line 5, column 1
Please check the below code:
Regards,
Mahesh
The error "System.QueryException: List has no rows for assignment to SObject:" means that your SOQL query is not returning any records. So either your Opportunity doesn't have an Account, or the way you are getting the Id isn't quite working.
As a side note, the way you have written the Trigger is not Bulkified (never put a SOQL query inside a for loop) and could cause issues during bulk uploads.
I suggest you look at @Mahesh's solution for how to proceed.
if(accMap.get(opp.AccountId) != null){