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
AzusfdcAzusfdc 

Here i am unable create a new record in after insert trigger related to opportunity line item selected?

Trigger OpportunityPrdHisTrack on OpportunityLineItem (after insert){
    
    if(Trigger.isafter && Trigger.isinsert)
    {
        HelperClass_one.opplineiteminsert(Trigger.new);
    }
}
 
public class HelperClass_one{
    public static void opplineiteminsert(List<OpportunityLineItem> opplinelist)
    {
        list<HistoryTracking__c> htlist=new list<HistoryTracking__c>();
        for(OpportunityLineItem oppline:opplinelist)
        {

            HistoryTracking__c ht=new HistoryTracking__c();
            ht.product__c=oppline.id;
            ht.Name=oppline.Name;
            ht.ProductDescription__c=oppline.Description;
            ht.ProductCode__c=oppline.ProductCode;
            htlist.add(ht);
            
        }
        insert htlist;
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Code look good to  me what error you are getting.

NOTE:- Record you are creating under OpportunityLineItem  not under Opportunity. Please check record under OpportunityLineItem 
AzusfdcAzusfdc
parent is opportunity of opportunitylineitem my task is when ever new opportunity line item is added to opportunity that should be tracked in history 
AzusfdcAzusfdc
Getting error like this:

Apex trigger OpportunityPrdHisTrack caused an unexpected exception, contact your administrator: OpportunityPrdHisTrack: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Product: id value of incorrect type: 00k28000004CdQhAAK: [Product__c]: Class.HelperClass_one.opplineiteminsert: line 16, column 1  
Amit Chaudhary 8Amit Chaudhary 8
issue is coming because of below line. Here you are passing OpportunityLineIteam ID in product lookup
ht.product__c=oppline.id;

try below line
ht.product__c=oppline.ProductId;
let us know if this will help you