You need to sign in to do that
Don't have an account?
shyam Sundar 101
Help me on trigger
When opportunity is closed won and based on the quantity which i give in opportunity line item .. The customer field QtyOnHand__c in Product2 should get changed with the amount of quantity which i select...
trigger RemainingQuant on OpportunityLineItem (after insert,before update) {
set<id> PIds = new set<id>();
set<id> Oids = new set<id>();
for(OpportunityLineItem opli : Trigger.new){
PIds.add(opli.Product2Id);
}
list<Product2> pList = [select id, name , QtyOnHand__c from Product2 where Id IN : PIds];
list<Opportunity> olist = [select id, name, StageName from Opportunity where Id IN : Oids];
for(Opportunity o : olist){
for(Product2 p : pList){
for(OpportunityLineItem op : Trigger.new){
if(p.id == op.product2Id && op.OpportunityId== o.id){
if(o.StageName == 'Closed Won'){
if(Trigger.isInsert){
if(op.Quantity > p.QtyOnHand__c){
op.Quantity.addError(' no availability of stock in Product ');
}
}
p.QtyOnHand__c-= op.Quantity;
}
if(Trigger.isUpdate){
p.QtyOnHand__c += (Trigger.oldMap.get(op.Id).Quantity).intValue();
if(op.Quantity > p.QtyOnHand__c){
op.addError(' no availability of stock in Product ');
}
p.QtyOnHand__c-=op.Quantity;
}
}
}
}
update pList;
}
}
trigger RemainingQuant on OpportunityLineItem (after insert,before update) {
set<id> PIds = new set<id>();
set<id> Oids = new set<id>();
for(OpportunityLineItem opli : Trigger.new){
PIds.add(opli.Product2Id);
}
list<Product2> pList = [select id, name , QtyOnHand__c from Product2 where Id IN : PIds];
list<Opportunity> olist = [select id, name, StageName from Opportunity where Id IN : Oids];
for(Opportunity o : olist){
for(Product2 p : pList){
for(OpportunityLineItem op : Trigger.new){
if(p.id == op.product2Id && op.OpportunityId== o.id){
if(o.StageName == 'Closed Won'){
if(Trigger.isInsert){
if(op.Quantity > p.QtyOnHand__c){
op.Quantity.addError(' no availability of stock in Product ');
}
}
p.QtyOnHand__c-= op.Quantity;
}
if(Trigger.isUpdate){
p.QtyOnHand__c += (Trigger.oldMap.get(op.Id).Quantity).intValue();
if(op.Quantity > p.QtyOnHand__c){
op.addError(' no availability of stock in Product ');
}
p.QtyOnHand__c-=op.Quantity;
}
}
}
}
update pList;
}
}
Here you need to write a trigger on Opportunity when the opportunity is updated.
Try this Trigger and handler class:
Trigger:
Handler:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Thanks for the reply but it work Opposite. If closed won the Quantity is getting added with the QtyOnhand__c..
Any one help me If the stage is closed lost I have to update with the previous value if the QtyOnHand__c field in product2
Can you please explain what is exactly your requirement.
If you want to do the same with when the opportunity Stage is Closed Lost, You have to change the condition in line number 9 in the handler class as:
if(op.StageName == 'Closed Lost' && OppOldMap.get(op.Id).StageName != 'Closed Lost'&& op.HasOpportunityLineItem)
Thanks,
Ajay Dubedi
Kindly have a look.
Thanks.
So in this case when the Opportunity is 'Closed Lost' then the number of quantity of Opportunity line item is added with the related Product QtyOnHand__c field.
As per your requirement, you need to write two triggers. 1st one is for Opportunity line item when the OpportunityLineItem is inserted and 2nd one is for Opportunity when the Opportunity is updated as it's stage is 'Closed Lost'.
Thanks,
Ajay Dubedi