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
RajanRajan 

Hi friends,I am getting an error for a trigger.

Hi friends,I am getting an error for a trigger. Condition is: I want to create an product whenever I am creating any opportunity. In opportunity, product is on the based of dependent picklist. Error is -  Error    Error: Compile Error: Entity is not api accessible at line 1 column 1

Code is as below:

trigger CreateProduct on Opportunity (after insert) {
for(Opportunity opp: trigger.new){
Product pod = new Product();
pod.Name = opp.Select_Product__c;

pod.Opportunityid = opp.id;
insert pod;
}
}
Rohit K SethiRohit K Sethi
Hi ,

Acutally there is no standard object Product that is Product2
That is why it raise error.

Thanks.
BALAJI CHBALAJI CH
Hi Rajan,

You are getting that error because the API name of Product is "Product2" in Salesforce. Please find below link for reference:
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_product2.htm

Let me know if that helps you.

Best Regards,
BALAJI
RAM AnisettiRAM Anisetti
Hi Rajan,

To add any product to opportunity,you must use OpportunityLineItem object.
please follow the below steps.
1)Create your product(API name of product is Product2)
2)Create your pricebook entry(API name of Pricebook Entry is PricebookEntry)
3)Create your opportunity line item(API name of Opportunity LineItem is OpportunityLineItem)

Your code look like below
//Create your product
Product2 prod = new Product2(
     Name = 'Product X',
     ProductCode = 'Pro-X',
     isActive = true
);
insert prod;

//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
     Pricebook2Id = pricebookId,(query your pricebook Id)
     Product2Id = prod.Id,
     UnitPrice = 100.00,
     IsActive = true
);
insert pbEntry;

//create your opportunity line item.  
OpportunityLineItem oli = new OpportunityLineItem(
     OpportunityId = opp.Id,
     Quantity = 5,
     PricebookEntryId = pbEntry.Id,
     
);
insert oli;


 
Swaraj Behera 7Swaraj Behera 7
Hi Rajan,
Please reference below code.
trigger X on Opportunity (before insert, after insert) {
    Pricebook2 standardBook = [SELECT Id FROM Pricebook2 WHERE IsStandard = true];
    if(Trigger.isBefore) {
        for(Opportunity record: Trigger.new) {
            record.Pricebook2Id = standardBook.Id;
        }
    }
    if(Trigger.isAfter) {
        OpportunityLineItem[] lines = new OpportunityLineItem[0];
        PricebookEntry entry = [SELECT Id, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = :standardBook.Id AND Product2.ProductCode = 'DEFAULT'];
        for(Opportunity record: Trigger.new) {
            lines.add(new OpportunityLineItem(PricebookEntryId=entry.Id, OpportunityId=record.Id, UnitPrice=entry.UnitPrice, Quantity=1));
        }
        insert lines;
    }
}

This is a rough estimate of the code you'll want to use. We need to set the price book before adding the product, so we do this in "before insert", then we add the product in the event "after insert". You may need to adjust the fields to suit your needs as well.

Thanks,
Swaraj