in account hierarchy we have parent,child and grandchild account...but i need to restrict parent account to create opportunities in which child and grandchild can create opportunity
Can you help me on understanding the this. To identfy that it is parent Opportunity we should check the field ParentAccount ==null which means that it does not have Parent Account. Let me know if my understanding is correct so i can provide you the logic for it.
Can you help me on understanding the this. To identfy that it is parent Opportunity we should check the field ParentAccount ==null which means that it does not have Parent Account. Let me know if my understanding is correct so i can provide you the logic for it.
Hi
which is the better approach, trigger or validation rule???
rigger Restrictparentaccount on Opportunity (before insert) {
set parentids = new set();
for(opportunity opp :trigger.new){
if(opp.accountid != Null){
parentids.add(opp.AccountId);
}
}
Map mapaccs = new map();
for(account acc:[select id,parentid,parent.parentId from account where parentId=:parentids OR parent.parentId=:parentids]){
mapaccs.put(acc.id,acc);
}
for(opportunity opp :trigger.new){
If(mapaccs.size()>0) {
opp.adderror('Opportunity cant be created by Parent Account');
}
}
}
If you are able to achieve it using Validation there is no point of going with Apex Trigger. We usually go with Trigger Validations if it is not possible by standard validation rules.
Can you help me on understanding the this. To identfy that it is parent Opportunity we should check the field ParentAccount ==null which means that it does not have Parent Account. Let me know if my understanding is correct so i can provide you the logic for it.
Thanks,
All Answers
Can you help me on understanding the this. To identfy that it is parent Opportunity we should check the field ParentAccount ==null which means that it does not have Parent Account. Let me know if my understanding is correct so i can provide you the logic for it.
Thanks,
You can just add this validation rule on the Opportunity object.
Let me know if you face any issues.
Thanks,
If you are able to achieve it using Validation there is no point of going with Apex Trigger. We usually go with Trigger Validations if it is not possible by standard validation rules.
Thanks,