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
Jameema ThamadapalliJameema Thamadapalli 

creating opportunity products based on parent Account opportunity product

Hi.. I have a usecase.Createing Opportunity products based on parent Account Opportunity produts.Any suggection on how i can do this?
1. Automated flows
2. triggers
how hard will it be catupre Parent Account,Opportunity Products and add them to Child Account(Opportunity Products).
ANUTEJANUTEJ (Salesforce Developers) 
Hi Jameema,

You could use the below link to call an apex method from a custom button or link that could get the related child records of the parent record and then insert them as a list.

>> https://help.salesforce.com/articleView?id=000325250&type=1&mode=1

Let me know if this helps or in case if there is any question.

Regards,
Anutej
Jameema ThamadapalliJameema Thamadapalli
Hi Anutej,
Thank you for the responce, but an looking for a way to add those Opportunity products from parent account to Child Account.
EX: I have a checkbox on Child Account when clicked,  It should create Opportunity products on Opprtunity. It should fetch the Opprtunity products based on parent Account Opportunity Product.
Every Account has only one Opportunity in this case.
Whats the best way to achive this?
1. Flows
2.Triggers
​​​​​​​3. whats the best practice? 
Abhishek BansalAbhishek Bansal
Hi Jameena,

This can be done with the help of the flows as well as triggers. If you go with the flow, the below link can help you:
http://chanukadissanayake.blogspot.com/2019/06/salesforce-auto-generate-renewal.html
You can modify the flow steps as per your requirement. 

If you go with triggers, you need to follow the below steps:
  1. Create a trigger on Opportunity. Context would be after insert
  2. Check if there is any Parent Opportunity available or not.
  3. Store the parent opp in a set and query the parent opp along with the child records.
  4. Now copy the child records into the newly inserted opps
Let me know if you need any further help on this.

Thanks,
Abhishek Bansal.
ANUTEJANUTEJ (Salesforce Developers) 
I think you should be able to do it with triggers as you would have more flexibility to the functionality you could use the below trigger code for reference , if this addresses your question can you please choose this as best answer so that it can be used by others in the future.
set<id> accid= new set<id>();
for(account a: trigger.new)
{
if(a.accountlookup!='' && a.checkbox== true)
{accid.add(accountlookup);
}}

list<accounts> lacc=[select id,[select id,oppname from opportunity] from account where id in :accid];
map<id,list<opportunity>> accoppmap= new map<id,list<opportunity>>();

for(Account lacc : lstAccount)
{
    accoppmap.put(acc.id, acc.Contacts);
}

map<id,list<opportunity>> moppn=new map<id,list<opportunity>>();
for(account a:trigger.new){
moppn.put(a.id,accoppmap.get(a.accountlookup));
}
Please make changes to the code as I wrote a general outline and not the exact implementation.

Also, in case if you are using a trigger make sure you are following the best practices so that the implementation is future proof.