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
emuelasemuelas 

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

Best Answer chosen by Admin (Salesforce Developers) 
User@SVFUser@SVF

 

 

 

All Answers

User@SVFUser@SVF

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, 

 

trigger iresv on Account(after update){
    if(Trigger.isUpdate){
        List<contact> clist = new List<contact>();
        for(Account m: Trigger.new) {
            //this field(m.test) is updated when a button is clicked
            if(m.new_check_box__c == TRUE) {
                Contact iv = new Contact();
                //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.LastName = 'Test Contact';
                clist.add(iv);
            }
        }
        try{
            if(!clist.isEmpty())
                insert clist;
        }catch(Exception e){
            //your exception handling actions.
        }
    }
}

 

Hope this helps solving your problem.

 

Prudhvi kamal.

 

 

rungerrunger

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.

emuelasemuelas

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


User@SVFUser@SVF

 

 

 

This was selected as the best answer
emuelasemuelas

Thank you so much Prudhvi!

 

This is working perfectly now..

 

Really appreciate it! ! Really helps a newcomer like me to understand these concepts