You need to sign in to do that
Don't have an account?
Nejib Essouri
Trigger that calculates total stock
Hi everyone, I am beginner of trigger development. I have 2 custom object 'Vente_c' & 'Tissus__c'. I would calculate the total stock of tissues after passing the sale. My code has no errors but nothing happens. Would you help me please..
trigger StockUpdateTissu on Vente__c (after delete, after insert, after update) { set<Id> set_Id = new set<Id>(); if(trigger.isInsert || trigger.isUpdate) for(Vente__c v : Trigger.new) set_Id.add(v.Tissus__c); if(trigger.isDelete) for(Vente__c v : Trigger.old) set_Id.add(v.Tissus__c); List<Tissus__c> listTissus = new List<Tissus__c>([SELECT ID FROM Tissus__c WHERE ID IN :set_Id]); List<Vente__c> listVente = new List<Vente__c>([SELECT ID FROM Vente__c WHERE ID IN :set_Id]); for(Tissus__c t : listTissus ){ t.Stock_actuel__c=500; for(Vente__c v : listVente ) t.Stock_actuel__c -= v.Metrage_tissus_vendu__c; } update listTissus; }
There were few small mistakes in your code which have been corrected by me.
Please replace your trigger with below code :
Let me know if you have any issue with the above code.
Thanks,
Abhishek
All Answers
try to use before event for trigger
As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
Thanks,
Alex
Maybe something like this:
There were few small mistakes in your code which have been corrected by me.
Please replace your trigger with below code :
Let me know if you have any issue with the above code.
Thanks,
Abhishek