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, assigned 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 Counting__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?

Thanks in advance

KS Kumaar

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;
    }
}
  • May i know why you have go for after update?? why not before update?/ – user34052 Jul 3 '16 at 8:30
  • The workflow will execute on all DML events - including Data Loader and hence will not count the # of edit button save actions. Furthermore, if using inline editing, there can be multiple save events without ever clicking Edit. Unclear of your requirement. – cropredy Jul 3 '16 at 17:17
1

Think you can avoid apex all together and just use workflow rule that always fires on upon insert and edit. Then create a field update that sets the integer field to the current value + 1.

  • Sorry for the late Reply. Thanks for you response @Benjamin Pirih, I can obey with your answer. But I want to do it using Trigger concept.. Please help me in this........! – user31765 Jul 1 '16 at 10:33
  • First rule of Salesforce Club.. you do not write code if you don't have to.. Second rule of Salesforce Club.. you DO NOT write code if you don't have to.. – Benjamin Pirih Jul 1 '16 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy