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
kaustubh chandratrekaustubh chandratre 

opportunity cannot be closed won inless there is atleast 1 opportunity product on opportunity

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Kaustubh,

Can you try the below apex trigger on opporutnity.
 
trigger closedopportunity on Opportunity (before update) {
    map<id,opportunity> oppymap= new map<id,opportunity>();
    for(opportunity opp:Trigger.new){
        if(opp.StageName=='Closed Won'){
            oppymap.put(opp.id, opp);
        }
        
    }
    if(!oppymap.isEmpty()){
        integer count = [SELECT count() FROM OpportunityLineItem WHERE opportunityid IN:oppymap.keySet() ] ;
        
        for(opportunity opp1:Trigger.new){
            if(count==0){
                opp1.adderror('atleast one product');
            }
        }

    }
}

If this solution helps, Please mark it as best answer.

Thanks,
​​​​​​​
CharuDuttCharuDutt
Hii Kaustabh
Try Below Trigger
trigger closedopportunity on Opportunity (before update) {
    map<id,opportunity> oppymap= new map<id,opportunity>();
    for(opportunity opp:Trigger.new){
        if(opp.StageName=='Closed Won'){
            oppymap.put(opp.id, opp);
        }
        
    }
    if(!oppymap.isEmpty()){
        integer count = [SELECT count() FROM OpportunityLineItem WHERE opportunityid IN:oppymap.keySet() ] ;
        
        for(opportunity opp1:Trigger.new){
            if(count==0){
                opp1.adderror('atleast one product');
            }
        }

    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!