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
Filipe BaradelliFilipe Baradelli 

expecting right curly bracket, found 'trigger'

I'm trying to create an Opportunity and insert it a product. It's the message that appears when I put the trigger : expecting right curly bracket, found 'trigger'. Someone can help me?
 
opp = new Opportunity();
        opp.Name = 'Test';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.UnitPrice =1000;
        oppLineItem.Quantity =1;
        insert oppLineItem;
        
        trigger CreateOLI on Opportunity (after insert) {
            List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
            List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE Product2Id= pr.Id];
            insert oliList;
        }
Filipe BaradelliFilipe Baradelli
pr = new Product2();
        pr.Name = 'PRODUTO TESTE2';
        pr.isActive=true;
        insert pr;
        
        opp = new Opportunity();
        opp.Name = 'Test';
        opp.CloseDate= System.Today();
        opp.StageName='Prospecting';
        insert opp;
        
        oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = opp.Id;
        oppLineItem.UnitPrice =1000;
        oppLineItem.Quantity =1;
        insert oppLineItem;
        
        trigger CreateOLI on Opportunity (after insert) {
            List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
            List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE Product2Id= pr.Id];
            insert oliList;
        }

I put the imcomplete code, here's the product pr being added.