0

My task is I need to count number of times a record has been edited using Edit button after record has been created.

For this I created a field called Edit_Counting__c (Number Type) and also created one workflow on MapTesting__c to get the count that record has been edited using ISCHANGED (Field Type).

Later I created one more field called Counting__c and I created this trigger,

Edit_counting__c with Counting__c (maap.Counting__c = maap.Edit_Counting__c+1) 

I am getting my output by using Counting__c value, but its working for only one record when I click edit button for one record respectively.

When I am trying to update more than one record using developer console, the Couting__c value does not effect to all the modified records. I do not know where I have done mistake. I also post my code below. Please check it and please could anyone help in this?

trigger MapTesting_Editable on MapTesting__c (after update) {

List<MapTesting__c> mmp= new List<MapTesting__c>();

List<MapTesting__c> mm = [select id, Edit_Counting__c,Counting__c from 
MapTesting__c where Id IN: Trigger.OldMap.keySet()];

    if(RecusrssionTrigger.flag){
        for(MapTesting__c maap:mm){
            RecusrssionTrigger.flag=false;
            maap.Counting__c = maap.Edit_Counting__c+1;
            mmp.add(maap);
        }
        update mmp;
    }
}
2
  • What does your test class look like? this context may only be 1 Trigger.newMap.keySet()
    – EricSSH
    Jun 30, 2016 at 5:26
  • How are you doing the "Bulk" and what is the deal with the recursion flag being what seems to be the revers to me. You are going to have to post more code for us to help. Also, review the debug logs as it will tell you exactly what it going on. How are you testing it to know it is not working?
    – Eric
    Jun 30, 2016 at 6:00

1 Answer 1

2

A before trigger would reduce the complexity a lot:

trigger MapTesting_Editable on MapTesting__c (before update) {

for(MapTesting__c map: trigger.new){
    maap.Counting__c = maap.Edit_Counting__c+1;

}

}

Is it just me or does your RecusrssionTrigger.flag seem to be backward in that it appears to be true before any recursion is possible?

You should also be checking the value of maap.Counting__c for null or you could get an error at that line

6
  • Hi Eric, My requirement is after update.
    – KS Kumaar
    Jun 30, 2016 at 5:30
  • Good catch on that flag Eric.. What is your use case? Is this a homework assignment? He's saying it doesn't need to be after update..
    – EricSSH
    Jun 30, 2016 at 5:31
  • 1
    Then question your requirement...because your requirement using up queries, heap, cpu time, etc needlessly. If you insist on the after update the only thing I can see is the weird use of the flag. When you say bulk how are you performing the "bulk operation"
    – Eric
    Jun 30, 2016 at 5:31
  • Yeah...... I am new Apex. Generally, i have done using before event. So now i am trying to do this using after update with more records.
    – KS Kumaar
    Jun 30, 2016 at 5:37
  • @KSKumaar Check this thread to see when to use before and after triggers generally.
    – Mr.Frodo
    Jun 30, 2016 at 5:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .