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
RavitejaRaviteja 

how to update all the Records using trigger

Hi,

I want update cost  field in raw custom obj.All records need to be update when i update one record.

 

I wrote some code its not working properly


trigger:

=========================

trigger costincr on Raw__c (before update) {

list<raw__c> raw= new list<raw__c>();
raw=[select id,name,cost__c,Exp_Date__c from Raw__c ];
cost_incr w=new cost_incr();
w.incr_method(raw);
}

class:

===================

public class cost_incr{


    public void incr_method(raw__c[] raw){
    raw=[select id,name,cost__c,Exp_Date__c from Raw__c ];
        for(raw__c rec:raw)
        {
        if(rec.cost__c!=null)
        {
        rec.cost__c=rec.cost__c+55;            
         }
        }
        update raw;
    }


}

 

Thanks

Anu Raj.ax1269Anu Raj.ax1269

Hi 

  I don't know whether i understood what you excatly need

  

  I think please try this 

  In this code add 

 

 trigger costincr on Raw__c (before update) {
        list<raw__c> raw= new list<raw__c>();
       raw=[select id,name,cost__c,Exp_Date__c from Raw__c ];
       //cost_incr w=new cost_incr();

       for(cost_incr w : Trigger.new){
       w.incr_method(raw);

      }
}

 

Thanks

Anu

   

Hengky IlawanHengky Ilawan

Hi,

 

You have a recursive trigger. Your before-update trigger call the class to update Raw__c records and it causes the same trigger to fire again.

 

See how to control a recursive trigger here:

http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm

 

Regards,

Hengky