function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sylvio AvillaSylvio 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:
 
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 



 
Best Answer chosen by Sylvio Avilla
viruSviruS
Please Try following

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

viruSviruS
Please Try following

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;
    }

}
This was selected as the best answer
Sylvio AvillaSylvio Avilla
Hello viruS,

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
 
viruSviruS
Hi Sylvio,

Sorry please update 

for( String keyId:oldMap){

to 

for( String keyId:oldMap.keySet()){
 
Sylvio AvillaSylvio Avilla
Hello viruS,

It worked! Thank you so much for your help!

Best

Sylvio
 
viruSviruS
Hi Sylvio,

Please mark it resolved!!