You need to sign in to do that
Don't have an account?
Update Trigger not working
I have a field "test__c" on an object order_line__c.
This field is updated to 1 when a button is clicked.
There is a trigger that is supposed to fire when this field is updated and it should create a new record.
However ,when the field is updated,there is no record creation.
The trigger code:
trigger iresv on order_line__c(after update)
{
if(Trigger.isUpdate){
for(Order_line__c m: Trigger.new)
{
//this field(m.test) is updated when a button is clicked
if(m.test__c == '1')
{
Inventory_Reserve__c iv = new Inventory_Reserve__c();
//new object that must be created :inventory reserve has a master detail with product_inventory custom object.
//m.pinv__c has the id of the product inventory master record.
iv.Product_Inventory__c= m.pinv__c;
insert iv;
}
}
}
}
Please help
emuelas,
you are doing an after update and trying to update the same record which could lead to an infinite loop and also on after trigger the record would be locked for most of the times. Instead run your trigger on a "before update" event. the code would be the same as it is now except that you have to exclude the update statement (update m.test__c)
one more thing, when you issue an update statement, you have to issue it on an object and not on a field i.e., your update statement should be update m;
All Answers
HI emuelas,
First of all I want you to check few options.
1. Is the trigger "Active"
2. Is the field test__c being set to 1 when you click on the button.
3. what is the data type of the field test__c , as you are checking it as a string. If it is a number then obviously the control comes out of if condition and is not going to create the record.
*** Never use DML statements in for loop, as they will cause Too many dml statement during bulk updates
Here is a sample code that works for your requirement,
Hope this helps solving your problem.
Prudhvi kamal.
Stab in the dark, but you're checking if m.test__c == '1', but you didn't say if the field type was a string or a number. If it's a numeric field, then comparing it to the string '1' will always be false.
Hi Prudhvi and Runger,
Thank you for the pointers.
It was the number and string problem,so i modified the test__c field to be a checkbox type field and wrote the following code,which is working fine.
But i need to update the m.test__c to False at the end of the iteration.When i add these two lines of code ,it errors out with the following error:
Error: Compile Error: DML requires SObject or SObject list type: Boolean at line 14 column 17
I tried changing it to update m ,but this throws an error as well that the record is read only....
I have highlighted these two lines of code.
Please help me out on this.
trigger iresv on Order_line__c(after update){
if(Trigger.isUpdate){
List<Inventory_Reserve__c> clist = new List<Inventory_Reserve__c>();
for(order_line__c m: Trigger.new) {
//this field(m.test) is updated when a button is clicked
if(m.test__c == TRUE) {
Inventory_Reserve__c iv = new Inventory_Reserve__c();
//new object that must be created :inventory reserve has a master detail with product_inventory custom object.
//m.pinv__c has the id of the product inventory master record.
iv.product_inventory__c = m.pid__c;
iv.order__c=m.order__c;
iv.reserved_quantity__c=m.quantity__c;
** m.test__c=FALSE;**error
**update m.test__c;**error
clist.add(iv);
}
}
if(!clist.isEmpty())
insert clist;
}
}
emuelas,
you are doing an after update and trying to update the same record which could lead to an infinite loop and also on after trigger the record would be locked for most of the times. Instead run your trigger on a "before update" event. the code would be the same as it is now except that you have to exclude the update statement (update m.test__c)
one more thing, when you issue an update statement, you have to issue it on an object and not on a field i.e., your update statement should be update m;
Thank you so much Prudhvi!
This is working perfectly now..
Really appreciate it! ! Really helps a newcomer like me to understand these concepts