+ Start a Discussion
Sfdc@SmithaSfdc@Smitha 

Find the error in the trigger

Hi

   Will the following trigger work ?can anybody will help me?

 

trigger tgrCounter on Mileage__c (after insert, after update) {

                List MilToupdate = new List();

                for(Mileage__c Mlg : Trigger.new){

                                Mlg.Counter__c = Mlg.Counter__c+1;

                                MilToupdate.add(Mlg);

                }

                update MilToupdate;

}

Best Answer chosen by Admin (Salesforce Developers) 
SFAdmin5SFAdmin5

write the second line like this

 

List<Mileage__c> MilToupdate = new List<Mileage__c>();

All Answers

SFAdmin5SFAdmin5

write the second line like this

 

List<Mileage__c> MilToupdate = new List<Mileage__c>();

This was selected as the best answer
SFAdmin5SFAdmin5

not exactly sure what you are trying to do but it appears you're just trying to add one to that counter field every time a mileage record is inserted or updated

 

use this for that

 

trigger tgrCounter on Mileage__c (before insert, before update)

{
    for(Mileage__c m: Trigger.new)
    {
        m.Counter__c = m.Counter__c+1;
    }
}

SFFSFF

You are trying to update a record in an after-trigger on this record. This should be done in a before-trigger.

 

Hope this helps,

Sfdc@SmithaSfdc@Smitha

Thanku for reply

SFAdmin5SFAdmin5

please mark this resolved if the trigger above works.  if not tell us why it won't meet your needs