You need to sign in to do that
Don't have an account?
Ankit 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.
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.
Please try this trigger -
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
I have gone through your question and came up with a trigger. Please try below code -
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
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
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. :)
Please try this trigger -
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
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,