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 Garg 117Ankit Garg 117 

Limit products on salesforce

Hi Gurus,

Is it possible to limit a perticular project on all opportunites? For example I have a product called factsheets. I want to limit that to 3. So only 3 times this product can be assigned to a opportunity. 

Note  - I am not talking aboutn limiting the number of products on 1 perticular opp but on al the opps. 

Is this possible via apex or trigger? if yes, can I please have a code. 

Thanks all. 
Best Answer chosen by Ankit Garg 117
Ajay K DubediAjay K Dubedi
Hi Ankit,

Please try this trigger - 
trigger LimitProduct on OpportunityLineItem (before insert, before update) {
    if(Trigger.isBefore) {
        if(Trigger.isInsert || Trigger.isUpdate) {
            list<OpportunityLineItem> oliList = new list<OpportunityLineItem>([Select Id, Quantity, OpportunityId, Product2Id From OpportunityLineItem]);
            oliList.addAll([Select Id, Quantity, OpportunityId From OpportunityLineItem Where Id In :Trigger.new]);         
            for(OpportunityLineItem oliN: Trigger.new) {
                decimal quantity = oliN.Quantity;
                for(OpportunityLineItem oli: oliList) {                         
                    if(oliN.OpportunityId ==  oli.OpportunityId && oliN.Product2Id == oli.Product2Id &&oliN.Id !=  oli.Id ) {                        
                        quantity += oli.Quantity;                    
                    }                    
                }
                if(quantity > 3) {                   
                    oliN.addError('One opportunity can only have three similar products!');
                }
            }        
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Ajay K DubediAjay K Dubedi
Hi Ankit,

I have gone through your question and came up with a trigger. Please try below code - 
 
Hi Ankit,

I have gone through your question and came up with a trigger. Please try below code - 

//Trigger
trigger LimitProductsQuantityTrigger on OpportunityLineItem (before insert, before update) {
    if(Trigger.isBefore) {
        if(Trigger.isInsert || Trigger.isUpdate) {
            LimitProductsQuantityHandler.limitProducts(Trigger.new);
        }
    }
}

//Handler Class
public class LimitProductsQuantityHandler {
    public static void limitProducts(list<OpportunityLineItem> oliListNew) {
        list<OpportunityLineItem> oliList = new list<OpportunityLineItem>([Select Id, Quantity, OpportunityId, Product2Id From OpportunityLineItem Where OpportunityId != Null]);
        oliList.addAll([Select Id, Quantity, OpportunityId From OpportunityLineItem Where OpportunityId != Null And Id In :oliListNew]);         
        for(OpportunityLineItem oliN: oliListNew) {
            decimal quantity = oliN.Quantity;
            for(OpportunityLineItem oli: oliList) {                         
                if(oliN.OpportunityId ==  oli.OpportunityId && oliN.Product2Id == oli.Product2Id &&oliN.Id !=  oli.Id ) {                        
                    quantity += oli.Quantity;                    
                }                    
            }
            if(quantity > 3) {                   
                oliN.addError('One opportunity can only have three similar products!');
            }
        }             
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Deepali KulshresthaDeepali Kulshrestha
Hi Ankit, 

I have gone through your question. I have made the code to limit the product on opportunity.

Please go through the code given below.


Trigger===>


trigger LimitProductsToOpportunityTrigger on Opportunity (after update) {
    if(trigger.isUpdate && trigger.isAfter){
        LimitProductsToOpportunity.limitProduct(trigger.new);
    }
}

Handler ==>

public class LimitProductsToOpportunity {
    public static void limitProduct(List<Opportunity> oppList){
        Set<Id>   opId = new Set<Id>();
        for(Opportunity op : oppList)  {
            if(op.HasOpportunityLineItem == true){
                opId.add(op.Id);
            }
        }
        List<OpportunityLineItem>   opliList  = [Select id,Name,product2Id,OpportunityId from Opportunitylineitem where OpportunityId in : opId];
        Map<ID,List<OpportunityLineItem>>  mapOfOppLineItemListAndOppId = new Map<ID,List<OpportunityLineItem>>();
        for(OpportunityLineItem opl : opliList ){
            if(mapOfOppLineItemListAndOppId.containsKey(opl.OpportunityId)){
                List<OpportunityLineItem>   oplst = mapOfOppLineItemListAndOppId.get(opl.OpportunityId);
                oplst.add(opl);
                mapOfOppLineItemListAndOppId.put(opl.OpportunityId,oplst);
            }
            else{
                mapOfOppLineItemListAndOppId.put(opl.OpportunityId,new List<OpportunityLineItem>());
                List<OpportunityLineItem>   oplst = mapOfOppLineItemListAndOppId.get(opl.OpportunityId);
                oplst.add(opl);
                mapOfOppLineItemListAndOppId.put(opl.OpportunityId,oplst);
            }
        }
        Map<ID,Integer>  mapOfProduct2IDAndNumberOfCountOfProduct = new Map<ID,Integer>();
        for(OpportunityLineItem opl : opliList){
            if(mapOfProduct2IDAndNumberOfCountOfProduct.containsKey(opl.Product2ID)){
                Integer count  = mapOfProduct2IDAndNumberOfCountOfProduct.get(opl.Product2ID);
                count++;
                mapOfProduct2IDAndNumberOfCountOfProduct.put(opl.Product2ID, count);
            }
            else{
                mapOfProduct2IDAndNumberOfCountOfProduct.put(opl.Product2ID, 0);
                Integer count  = mapOfProduct2IDAndNumberOfCountOfProduct.get(opl.Product2ID);
                count++;
                mapOfProduct2IDAndNumberOfCountOfProduct.put(opl.Product2ID, count);
            }
        }
        for(Opportunity op : oppList){
            if(opId.contains(op.Id)){
                List<OpportunityLineItem>   opplineItemList = mapOfOppLineItemListAndOppId.get(op.Id);
                for(OpportunityLineItem  opl  : opplineItemList){
                    Integer count = mapOfProduct2IDAndNumberOfCountOfProduct.get(opl.product2Id);
                    if(count >= 4){
                        op.adderror('One Product Cannot be more than 3 times');
                    }
                }
            }
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com



 
Ankit Garg 117Ankit Garg 117
Thanks Ajay and Deepali, Really appriciate your efforts. 

I am getting the following error while trying the code. Also, complier doesn't show me all the errors so not sure if there are any other errors.


For Ajay - I am getting this error - Error: Compile Error: Missing '<EOF>' at 'public' at line 11 column 1

For Deepali - I am getting this error - Error: Compile Error: Missing '<EOF>' at 'Handler' at line 7 column 1

Please advise. :)
 
Ajay K DubediAjay K Dubedi
Hi Ankit,

Please try this trigger - 
trigger LimitProduct on OpportunityLineItem (before insert, before update) {
    if(Trigger.isBefore) {
        if(Trigger.isInsert || Trigger.isUpdate) {
            list<OpportunityLineItem> oliList = new list<OpportunityLineItem>([Select Id, Quantity, OpportunityId, Product2Id From OpportunityLineItem]);
            oliList.addAll([Select Id, Quantity, OpportunityId From OpportunityLineItem Where Id In :Trigger.new]);         
            for(OpportunityLineItem oliN: Trigger.new) {
                decimal quantity = oliN.Quantity;
                for(OpportunityLineItem oli: oliList) {                         
                    if(oliN.OpportunityId ==  oli.OpportunityId && oliN.Product2Id == oli.Product2Id &&oliN.Id !=  oli.Id ) {                        
                        quantity += oli.Quantity;                    
                    }                    
                }
                if(quantity > 3) {                   
                    oliN.addError('One opportunity can only have three similar products!');
                }
            }        
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer
Ankit Garg 117Ankit Garg 117
This works :)

Thanks Ajay for all your time and effort. Also, can you please guide me what would be the best place to learn these codes?


cheers,