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
Nandhini GovindrajNandhini Govindraj 

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

Best Answer chosen by Nandhini Govindraj
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Nandhini,

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

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Nandhini,

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,
 
This was selected as the best answer
Nandhini GovindrajNandhini Govindraj
Hi ya understood.. thank you.. provide me the logic
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

You can just add this validation rule on the Opportunity object.
 
ISBLANK(Account.ParentId)

Let me know if you face any issues.


Thanks,
Nandhini GovindrajNandhini Govindraj
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'); } } }
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Nandhini,

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,
 
Nandhini GovindrajNandhini Govindraj
ok Thank you.. i have given you the code for trigger ,in that can you give me some idea in that SQL query..