You need to sign in to do that
Don't have an account?
Lukesh Karmore
help me with this trigger below
i m writing trigger, created custom field on product object Total Quantity and Available Quantity .
i have to fill Available Quantity field when opportunityLineItem is Created
code is successfully deployed but not update Available Quantity field
trigger AvailableQuantityonProductTrigger on OpportunityLineitem (after insert) {
integer remainingQuantity=0;
set<id> oliIds=new set<id>();
for(OpportunityLineitem o:Trigger.new){
if(o.Quantity!=null){
oliIds.add(o.id);
}
}
list<OpportunityLineitem> oliList=[SELECT id,Quantity,Product2.Name,Product2.Total_Quantity__c,
Product2.Available_Quantity__c FROM OpportunityLineitem WHERE id IN:oliIds];
for(OpportunityLineitem oli:oliList){
if(oli.Quantity!=null){
remainingQuantity=integer.valueOf(oli.Product2.Total_Quantity__c) - integer.valueOf(oli.Quantity);
}
else{
remainingQuantity=0;
}
if(remainingQuantity!=null){
oli.Product2.Available_Quantity__c=integer.valueOf(remainingQuantity);
}
}
update oliList;
}
Thank you
i have to fill Available Quantity field when opportunityLineItem is Created
code is successfully deployed but not update Available Quantity field
trigger AvailableQuantityonProductTrigger on OpportunityLineitem (after insert) {
integer remainingQuantity=0;
set<id> oliIds=new set<id>();
for(OpportunityLineitem o:Trigger.new){
if(o.Quantity!=null){
oliIds.add(o.id);
}
}
list<OpportunityLineitem> oliList=[SELECT id,Quantity,Product2.Name,Product2.Total_Quantity__c,
Product2.Available_Quantity__c FROM OpportunityLineitem WHERE id IN:oliIds];
for(OpportunityLineitem oli:oliList){
if(oli.Quantity!=null){
remainingQuantity=integer.valueOf(oli.Product2.Total_Quantity__c) - integer.valueOf(oli.Quantity);
}
else{
remainingQuantity=0;
}
if(remainingQuantity!=null){
oli.Product2.Available_Quantity__c=integer.valueOf(remainingQuantity);
}
}
update oliList;
}
Thank you
Check this code it may help you.
//Class //Trigger If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi
I see what you try to do: you want to update the OLIs using 'update oliList'. However you need to update the products, not the OLIs.
So something like this:
List<OpportunityLineItem> oliList = [SELECT Id, Product2, Quantity FROM OpportunityLineItem WHERE Id IN :oliIds];
Set<Id> productIds = new Set<Id>();
Map<Id, Integer> productIdToOLIQuantityMap = new Map<Id, Integer>();
for(OpportunityLineItem o : oliList) {
productIds.add(o.Product2);
productIdToOLIQuantityMap.put(o.Product2, o.Quantity);
}
List<Product2> products = [SELECT Id, Available_Quantity__c, Total_Quantity__c FROM Product2 WHERE Id IN :productIds];
for(Product2 p : products) {
p.Available_Quantity__c = p.Total_Quantity__c - productIdToOLIQuantityMap.get(p.Id);
}
update products;
Make sure to mark as best answer if it helps you :)
Please find the Working Code.
Thanks,
Vijay,
Solution Architect,
Steadfast Consultancy Services
trigger MaintainAvailableQuantity_AfterDeleteOpportunityLineItem on OpportunityLineitem (after delete) {
set<id> OliIds=new set<id>();
for(OpportunityLineitem o:Trigger.old){
OliIds.add(o.id);
}
list<OpportunityLineitem> ItemList=[SELECT Quantity,product2.Available_Quantity__c,product2.Total_Quantity__c FROM OpportunityLineitem WHERE id IN :OliIds];
set<id> productIds=new set<id>();
map<Id,Decimal> productMapIds=new map<Id,Decimal>();
for(OpportunityLineitem i:ItemList ){
productIds.add(i.product2Id);
productMapIds.put(i.product2Id,i.Quantity);
}
list<product2> productList=[SELECT Id,Total_Quantity__c,Available_Quantity__c FROM Product2 WHERE Id IN:productIds];
for(product2 p:productList){
p.Available_Quantity__c=Integer.valueOf(p.Available_Quantity__c)+Integer.valueOf(productMapIds.get(p.id));
}
update productList;
}
Thank you
Please find the Working Code.
Thanks,
Vijay,
Solution Architect,
Steadfast Consultancy Services
If it resolved your Issue please mark it as the best answer.
Thanks,
Vijay,
Solution Architect,
Steadfast Consultancy Services