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
Hemant Mittal 2Hemant Mittal 2 

Write a trigger on Opportunity, when an Opportunity will be insert an Opportunity Line Item should be insert by default with any of the Product associated with Opportunity Issue

when i check in schema builder then also i don't see any relationship between OpportunityLineItem and PriceBookEntry

But in object manager, when i check OpportunityLineItem object it doesn't have any field or relationship named PricebookEntryIdUser-added image
Best Answer chosen by Hemant Mittal 2
AbhinavAbhinav (Salesforce Developers) 
Hi Hemant,

Please check below link which will help you to understand  the relationship 

https://salesforce.stackexchange.com/questions/107314/relationship-between-opportunity-opportunitylineitemsopportunity-product-pri

Thanks!

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Hemant,

Please check below link which will help you to understand  the relationship 

https://salesforce.stackexchange.com/questions/107314/relationship-between-opportunity-opportunitylineitemsopportunity-product-pri

Thanks!
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47

Hi Hemant,

Please find the solution.

trigger opportunityData on Opportunity (after insert) {


list<opportunityLineItem> olilist= new list<OpportunityLineItem>();
    List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE PriceBook2.isStandard=true LIMIT 1];
    for(opportunity op:Trigger.New){
        OpportunityLineItem oli= New OpportunityLineItem();
        oli.OpportunityId = op.ID;
        oli.PricebookEntryId = priceBookList[0].Id;
		oli.UnitPrice = 7000;
        oli.Quantity = 5;
        olilist.add(oli);
    }
    if(olilist!=null&&olilist.size()>0)
        insert olilist;
      

}

Please mark it as the Best Answer so that other people would take references from it.

Thank You

Hemant Mittal 2Hemant Mittal 2
Hi Abhinav,
I checked the link given by you.
But it doesn't answer my question that why we can't see this relationship in schema builder.
 
Suraj Tripathi 47Suraj Tripathi 47

Hi Abhinav,

Please follow the link below you can definitely get your answer.

https://jayakrishnasfdc.wordpress.com/2019/12/22/products-pricebooks-pricebook-entries-in-salesforce/

If you need to add new fields to Opportunity Line Items then Opportunity Product is the place. Basically Opportunity Product is same as Opportunity Line Item. It's just that in Salesforce the object name that is to be used in triggers etc for Opportunity Line Items is OpportunityLineItem and the UI name is Opportunity Products.

So to add new fields to line items for to Customize -> Opportunities -> Opportunity Products -> Fields.
And to add a new trigger to Opportunity Line Items you can also go to Customize -> Opportunities -> Opportunity Products -> Triggers, other then the two ways which I shared with you earlier.

Please mark it as the Best Answer if it helps you .

Thank You