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
Cesar Ramirez Vasquez005391619375684564Cesar Ramirez Vasquez005391619375684564 

Create a trigger to update Object Product field Quantity after an invoice line is inserted ?

How can i achieve that this are the fields in object Line
Cantidad__c   Number(4, 0)  
Factura__c     Master-Detail(Factura)
Producto__c    Lookup(Producto)
Monto_Colones__c        Currency(16, 2)

I need to Update this fiel from Product__c
CantidadExistencia__c  Number(4, 0)

Basically i want to set CantidadExistencia__c = CantidadExistencia__c - Line.Cantidad__c for each line i insert; this is the code that insert every line:
public void insertarLinea()
    {
       
        try
        {
        List<Linea__c> lines = new list<Linea__c>(); // crea una lista con las lineas de la factura
            //Para cada producto C en carrito
            for(Producto__c C : CARRITO) {

              for(Integer i =0; i < CARRITO.size();i++){ // Creamos un loop para que recorra toda la lista CARRITO y los integre a una nueva lista de lineas
                 
                   Linea__c Line = new Linea__c ();
                   Line.Cantidad__c = C.CantidadExistencia__c;
                   Line.Factura__c = current_factura.id;                 
                   Line.Monto_Colones__c = C.Precio__c;

               
                   Line.Producto__c = C.id;
 
                   lines.add(Line); // adding line to list
                                          }

                                     }

            insert (lines); // Insertamos la lista lineas asi evitamos hacer un insert por cada registro que haya en CARRITO
        }
       
        catch(Exception a)
       
        {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error al insertar la linea. '+a));
        }
       
       
    }
Best Answer chosen by Cesar Ramirez Vasquez005391619375684564
ShashForceShashForce
Please try something like this. I have marked the lines I added as "//added line of code". You might have to make minor changes, but I hope it gives you a good idea of what to do.

public void insertarLinea()
    {
      
        try
        {
        List<Linea__c> lines = new list<Linea__c>(); // crea una lista con las lineas de la factura
  List<Product__c> ProductsToUpdate = new list<Product__c>(); //added line of code
  //Para cada producto C en carrito
            for(Producto__c C : CARRITO) {
    integer num = C.CantidadExistencia__c; //added line of code
              for(Integer i =0; i < CARRITO.size();i++){ // Creamos un loop para que recorra toda la lista CARRITO y los integre a una nueva lista de lineas
                
                   Linea__c Line = new Linea__c ();
                   Line.Cantidad__c = C.CantidadExistencia__c;
                   Line.Factura__c = current_factura.id;                
                   Line.Monto_Colones__c = C.Precio__c;
       num = num - Line.Cantidad__c; //added line of code
              
                   Line.Producto__c = C.id;
    
                   lines.add(Line); // adding line to list
                                          }
    C.CantidadExistencia__c = num; //added line of code
    ProductsToUpdate.add(C); //added line of code
   
                                     }

            insert (lines); // Insertamos la lista lineas asi evitamos hacer un insert por cada registro que haya en CARRITO
   update(C); //added line of code
        }
      
        catch(Exception a)
      
        {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error al insertar la linea. '+a));
        }
      
      
    }

All Answers

ShashForceShashForce
Please try something like this. I have marked the lines I added as "//added line of code". You might have to make minor changes, but I hope it gives you a good idea of what to do.

public void insertarLinea()
    {
      
        try
        {
        List<Linea__c> lines = new list<Linea__c>(); // crea una lista con las lineas de la factura
  List<Product__c> ProductsToUpdate = new list<Product__c>(); //added line of code
  //Para cada producto C en carrito
            for(Producto__c C : CARRITO) {
    integer num = C.CantidadExistencia__c; //added line of code
              for(Integer i =0; i < CARRITO.size();i++){ // Creamos un loop para que recorra toda la lista CARRITO y los integre a una nueva lista de lineas
                
                   Linea__c Line = new Linea__c ();
                   Line.Cantidad__c = C.CantidadExistencia__c;
                   Line.Factura__c = current_factura.id;                
                   Line.Monto_Colones__c = C.Precio__c;
       num = num - Line.Cantidad__c; //added line of code
              
                   Line.Producto__c = C.id;
    
                   lines.add(Line); // adding line to list
                                          }
    C.CantidadExistencia__c = num; //added line of code
    ProductsToUpdate.add(C); //added line of code
   
                                     }

            insert (lines); // Insertamos la lista lineas asi evitamos hacer un insert por cada registro que haya en CARRITO
   update(C); //added line of code
        }
      
        catch(Exception a)
      
        {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error al insertar la linea. '+a));
        }
      
      
    }
This was selected as the best answer
Cesar Ramirez Vasquez005391619375684564Cesar Ramirez Vasquez005391619375684564
Thanks for your reply, i already solved it ! but that was the answer.