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
K_devK_dev 

Help me with the test class for my trigger

 

trigger create_oli on Opportunity (after insert) 
{
    list<OpportunityLineItem> lst_oli=new list<OpportunityLineItem >();     
    map<string,string> pdcts=new map<string,string>();
    for(Opportunity o:Trigger.New)
    {
        if(o.Product_Id__c!=null)
        {
            pdcts.put(o.Id,o.Product_Id__c);            
        }
    }
    if(!pdcts.IsEmpty())
    {
        map<String,string> pbe_map=new map<string,string>();
        list<pricebookentry> pbe=[select id, Product2Id  from PricebookEntry where Product2Id IN:pdcts.values()];
        for(pricebookentry p:pbe)
        {
            pbe_map.put(p.Product2Id,p.Id);
        }
        for(string s:pdcts.Keyset())
        {
            if(pbe_map.containsKey(pdcts.get(s)))
            {
                OpportunityLineItem oli=new OpportunityLineItem();
                oli.Opportunityid=s;
                //oli.Product2=o.Product_Id__c;
                oli.Quantity = 1;
                oli.PricebookEntryId=pbe_map.get(pdcts.get(s));
                oli.TotalPrice=1000;
                lst_oli.add(oli);
            }
        }
        if(!lst_oli.IsEmpty())
        {
            insert lst_oli;
        }
    }
}