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
Ankit Kumar 169Ankit Kumar 169 

How to write a Trigger to stop add more than 2 Opportunity line Items records in an Opportunity.

Apex code ::
public class Trg11HelperClass {
    public static void addLineItem(List<Opportunity> opList){
        
            if(opList.Size()>0){
    List<OpportunityLineItem> Oli = [select Description,ListPrice,Name,OpportunityId from 
                                     OpportunityLineItem where  OpportunityId IN : opList];
                 
              for(Opportunity op:opList){
                  Integer count =0;
              for(OpportunityLineItem o:oli)  {
                 
                  count++;
                
                }  
                  if(count > 2){
                        op.addError('opportunity cannot be closed');
                           } 
                
            }
        }
    }
}

Trigger::

trigger Trg11 on Opportunity (after insert) {
    if(trigger.isUpdate)
Trg11HelperClass.addLineItem(Trigger.new);
}
Duke_SfdcDuke_Sfdc
Hi Ankit,

Please find below a code that will fulfill your requirement:

trigger addOnlyTwoOpportunityLineItem on OpportunityLineItem (Before Insert,Before Update,Before Delete) {
    
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)){
        list<OpportunityLineItem> oppLineitemList= (trigger.isInsert || trigger.isUpdate)?trigger.new:trigger.old;
        Set<string> opportunityIdSet=new set<string>();
        
        for(OpportunityLineItem opl:oppLineitemList){
            if(opl.opportunityId!=null)
                opportunityIdSet.add(opl.opportunityId);
        }
        
        map<string,integer> countRelatedOpportunityLineItemMap=new map<string,integer>();
        for(aggregateResult ar:[select opportunityId oid,count(id) cid from OpportunityLineItem 
                                where opportunityId in:opportunityIdSet group by opportunityId]){
            countRelatedOpportunityLineItemMap.put(string.valueOf(ar.get('oid')),integer.valueOf(ar.get('cid')));
        }
        
        for(OpportunityLineItem opl:oppLineitemList){
            if(opl.opportunityId!=null){
                if(countRelatedOpportunityLineItemMap.containsKey(opl.opportunityId)){
                    integer count=countRelatedOpportunityLineItemMap.get(opl.opportunityId);
                    if(count>1){
                        opl.addError('You are not allow to create more than 2 LineItems');
                    }
                    countRelatedOpportunityLineItemMap.put(opl.opportunityId,count);
                }
                else if(opl.opportunityId!=null && !countRelatedOpportunityLineItemMap.containsKey(opl.opportunityId)){
                    countRelatedOpportunityLineItemMap.put(string.valueOf(opl.opportunityId),1);
                }
            }
        }
    }
}

Please mark it as solved if this will fulfill your requirement.

Thanks
Duke_Sfdc