You need to sign in to do that
Don't have an account?
Sylvio Avilla
"Undo" a Workflow
Hello Everyone,
To simplify, I would like to create something similar to : a trigger to reverse the "Update Total Inventory When an Order is Placed" at the "Create a Warehouse App" example. That mans, when I delete a Invoice, it would subtract the amount of items on the inventory!
Reference : https://developer.salesforce.com/docs/atlas.en-us.workbook.meta/workbook/workflow_4.htm
Total_Inventory_Quantity = Total_Inventory_Quantity - Invoice_Quantity.
I could find something that is similar to what I want, at this post :http://salesforce.stackexchange.com/questions/22967/trigger-to-update-account-running-total-upon-change-to-any-child-custom-invoice, and end up with this code:
Cloud anyone help me on this?
Thanks
To simplify, I would like to create something similar to : a trigger to reverse the "Update Total Inventory When an Order is Placed" at the "Create a Warehouse App" example. That mans, when I delete a Invoice, it would subtract the amount of items on the inventory!
Reference : https://developer.salesforce.com/docs/atlas.en-us.workbook.meta/workbook/workflow_4.htm
Total_Inventory_Quantity = Total_Inventory_Quantity - Invoice_Quantity.
I could find something that is similar to what I want, at this post :http://salesforce.stackexchange.com/questions/22967/trigger-to-update-account-running-total-upon-change-to-any-child-custom-invoice, and end up with this code:
trigger Cancelar_Item_de_Nota on Itens_da_Nota_Fiscal__c (after delete, before update) { if(Trigger.isDelete) //&& (Trigger.isInsert || Trigger.isUpdate)){ CancelarItemDaNota.atualizarQuantidadeemEstoque(); }
public class CancelarItemDaNota { public static void atualizarQuantidadeemEstoque() { Set<Itens_da_Nota_Fiscal__c> invoices = new Set<Itens_da_Nota_Fiscal__c>(); Set<Id> accountIds = new Set<Id>(); List<Item__c> accounts = [SELECT Id, Quantidade_em_Estoque__c, (SELECT Id, Quantidade__c FROM Itens_da_Nota_Fiscal__r) FROM Item__c WHERE Id IN :accountIds]; for(Itens_da_Nota_Fiscal__c invoice:invoices){ // accountIds.add(invoice.Item__c); } for(Item__c acc:accounts){ // acc.Quantidade_em_Estoque__c = 0; for(Itens_da_Nota_Fiscal__c invoice:acc.Itens_da_Nota_Fiscal__r){ acc.Quantidade_em_Estoque__c -= invoice.Quantidade__c ; } } update accounts; } }So,The code runs without any error but field is not updated. Nothing happens.
Cloud anyone help me on this?
Thanks
trigger Cancelar_Item_de_Nota on Itens_da_Nota_Fiscal__c (after delete, before update) {
if(Trigger.isDelete) //&& (Trigger.isInsert || Trigger.isUpdate)){
CancelarItemDaNota.atualizarQuantidadeemEstoque(trigger.OldMap);
}
}
public class CancelarItemDaNota {
public static void atualizarQuantidadeemEstoque(Map<Id,Itens_da_Nota_Fiscal__c> oldMap) {
Set<Itens_da_Nota_Fiscal__c> invoices = new Set<Itens_da_Nota_Fiscal__c>();
Set<Id> accountIds = new Set<Id>();
for( String keyId:oldMap){
accountIds.add(oldMap.get(keyId).Item__c);
}
List<Item__c> accounts = [SELECT Id, Quantidade_em_Estoque__c, (SELECT Id, Quantidade__c FROM Itens_da_Nota_Fiscal__r) FROM Item__c WHERE Id IN :accountIds];
for(Item__c acc:accounts){
for(Itens_da_Nota_Fiscal__c invoice:acc.Itens_da_Nota_Fiscal__r){
acc.Quantidade_em_Estoque__c -= invoice.Quantidade__c ;
}
}
update accounts;
}
}
All Answers
trigger Cancelar_Item_de_Nota on Itens_da_Nota_Fiscal__c (after delete, before update) {
if(Trigger.isDelete) //&& (Trigger.isInsert || Trigger.isUpdate)){
CancelarItemDaNota.atualizarQuantidadeemEstoque(trigger.OldMap);
}
}
public class CancelarItemDaNota {
public static void atualizarQuantidadeemEstoque(Map<Id,Itens_da_Nota_Fiscal__c> oldMap) {
Set<Itens_da_Nota_Fiscal__c> invoices = new Set<Itens_da_Nota_Fiscal__c>();
Set<Id> accountIds = new Set<Id>();
for( String keyId:oldMap){
accountIds.add(oldMap.get(keyId).Item__c);
}
List<Item__c> accounts = [SELECT Id, Quantidade_em_Estoque__c, (SELECT Id, Quantidade__c FROM Itens_da_Nota_Fiscal__r) FROM Item__c WHERE Id IN :accountIds];
for(Item__c acc:accounts){
for(Itens_da_Nota_Fiscal__c invoice:acc.Itens_da_Nota_Fiscal__r){
acc.Quantidade_em_Estoque__c -= invoice.Quantidade__c ;
}
}
update accounts;
}
}
Thanks for your help.
I change the code but got the following error
Loop must interate over a collection type: Map Line 5
Any clue what may be the problem?
Thanks again
Sorry please update
for( String keyId:oldMap){
to
for( String keyId:oldMap.keySet()){
It worked! Thank you so much for your help!
Best
Sylvio
Please mark it resolved!!