You need to sign in to do that
Don't have an account?
Cesar Ramirez Vasquez005391619375684564
Im writting a trigger after insert a line with a product, i want to deduct the quantity of that line to the existing quantity in object Product__c ?
How can i achieve that im pretty new to apex basiclly i want to execute the following sql code everytime a line is inserted:
update Producto__c set CantidadExistencia__c = Producto__c.CantidadExistencia__c - Linea__c.Cantidad__c where Linea__c.Producto__c = Producto__c.id
I tried to write and my trigger looks like this:
trigger ActualizaExistencia on Linea__c(before insert, before update){
//get a list of all the products ID's contained in the records
//to be updated.
Set<Id> ids = trigger.newmap.keySet();
List<Linea__c> inv=[select id,Cantidad__c from Linea__c where id IN :ids];
List<Producto__c> ProIDs = new List<Producto__c>();
for (Linea__c Ite:trigger.new){
ProIDs.add(Ite.Producto__c);
}
//now get a list of all the records for products that contain the
//above IDs
List<Producto__c > Pros = new List<Producto__c >([select id, CantidadExistencia__c from Producto__c
where id in:ProIDs]);
//now loop again for all the records being updated and then for each
//one loop through all the products records retrieved above.
for (Linea__c obj: trigger.new){
//we do this for loop differently so that it has an inherent check
//to ensure that our query above returned some records
for (integer i = 0; i < Pros.size(); i++){
//now we make sure the record IDs match
if (Pros.Producto__c == pros[i].id){
Pros[i].CantidadExistencia__c = Pros[i].CantidadExistencia__c - inv[i].Cantidad__c ;
}
}
}
//update all the products records
update Pros;
}
Is not working, i know something are really bad like trying to do this Pros[i].CantidadExistencia__c = Pros[i].CantidadExistencia__c - inv[i].Cantidad__c ;
What can i do, Thanks in advance !
update Producto__c set CantidadExistencia__c = Producto__c.CantidadExistencia__c - Linea__c.Cantidad__c where Linea__c.Producto__c = Producto__c.id
I tried to write and my trigger looks like this:
trigger ActualizaExistencia on Linea__c(before insert, before update){
//get a list of all the products ID's contained in the records
//to be updated.
Set<Id> ids = trigger.newmap.keySet();
List<Linea__c> inv=[select id,Cantidad__c from Linea__c where id IN :ids];
List<Producto__c> ProIDs = new List<Producto__c>();
for (Linea__c Ite:trigger.new){
ProIDs.add(Ite.Producto__c);
}
//now get a list of all the records for products that contain the
//above IDs
List<Producto__c > Pros = new List<Producto__c >([select id, CantidadExistencia__c from Producto__c
where id in:ProIDs]);
//now loop again for all the records being updated and then for each
//one loop through all the products records retrieved above.
for (Linea__c obj: trigger.new){
//we do this for loop differently so that it has an inherent check
//to ensure that our query above returned some records
for (integer i = 0; i < Pros.size(); i++){
//now we make sure the record IDs match
if (Pros.Producto__c == pros[i].id){
Pros[i].CantidadExistencia__c = Pros[i].CantidadExistencia__c - inv[i].Cantidad__c ;
}
}
}
//update all the products records
update Pros;
}
Is not working, i know something are really bad like trying to do this Pros[i].CantidadExistencia__c = Pros[i].CantidadExistencia__c - inv[i].Cantidad__c ;
What can i do, Thanks in advance !
try below code..
trigger ActualizaExistencia on Linea__c(before insert, before update){
//get a list of all the products ID's contained in the records
//to be updated.
Set<Id> ids = trigger.newmap.keySet();
List<Linea__c> inv=[select id,Cantidad__c from Linea__c where id IN :ids];
List<Producto__c> ProIDs = new List<Producto__c>();
for (Linea__c Ite:trigger.new){
ProIDs.add(Ite.Producto__c);
}
//now get a list of all the records for products that contain the
//above IDs
List<Producto__c > Pros = new List<Producto__c >([select id, CantidadExistencia__c from Producto__c
where id in:ProIDs]);
//now loop again for all the records being updated and then for each
//one loop through all the products records retrieved above.
for (Linea__c obj: trigger.new){
//we do this for loop differently so that it has an inherent check
//to ensure that our query above returned some records
//now we make sure the record IDs match
if (Pros[0].Producto__c == pros[0].id){
Pros[0].CantidadExistencia__c = Pros[0].CantidadExistencia__c - inv[0].Cantidad__c ;
}
}
//update all the products records
update Pros;
}
Thanks,
Rockzz
Error: Compile Error: Incompatible element type Id for collection of SOBJECT:Producto__c at line 10 column 4
trigger ActualizaExistencia on Linea__c(before insert, before update){
//get a list of all the products ID's contained in the records
//to be updated.
Set<Id> ids = trigger.newmap.keySet();
List<Linea__c> inv=[select id,Cantidad__c from Linea__c where id IN :ids];
Set<Id> ProIDs = new Set<Id>();
for (Linea__c Ite:trigger.new){
ProIDs.add((ID)Ite.get('Producto__c'));
}
//now get a list of all the records for products that contain the
//above IDs
List<Producto__c > Pros = new List<Producto__c >([select id, CantidadExistencia__c from Producto__c
where id in:ProIDs]);
//now loop again for all the records being updated and then for each
//one loop through all the products records retrieved above.
for (Linea__c obj: trigger.new){
//we do this for loop differently so that it has an inherent check
//to ensure that our query above returned some records
for (integer i = 0; i < Pros.size(); i++){
//now we make sure the record IDs match
if (Pros.Producto__c == pros[i].id){
Pros[i].CantidadExistencia__c = Pros[i].CantidadExistencia__c - inv[i].Cantidad__c ;
}
}
}
//update all the products records
update Pros;
}
Thanks,
Rockzz
Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Producto__c> at line 23 column 11
That code clearly dont work