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
KS KumaarKS Kumaar 

I have a trigger and I am freezing at working with more than one record on Custom object

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; } }
TintuBabuTintuBabu
Hi
To capture no of edits made on a record i dont thing you need both workflow and trigger.
Create one Number field to track count. Say Edit_Counting__c

just write a trigger.

trigger EditsCount on Account (before Update) {
    for(Account ff : Trigger.New){
        if(ff.Edit_Counting__c == null){
            ff.Edit_Counting__c = 0;
        }
        ff.Edit_Counting__c = ff.Edit_Counting__c+1;
    }

}

if you need to track count only for the edit of particular field say country__c

trigger EditsCount on Account (before Update) {
    for(Account ff : Trigger.New){
     if(ff.country__c !=  Trigger.oldMap.get(ff.id).country__c){
        if(ff.Edit_Counting__c == null){
            ff.Edit_Counting__c = 0;
        }
        ff.Edit_Counting__c = ff.Edit_Counting__c+1;
    }
}

}
James LoghryJames Loghry
I think the issue is your Edit_Count__c field is never getting incremented.  In other words, it looks like you're updating the multiple records correctly in your trigger, but the Edit_Counting__c field is not getting updated prior to your trigger running.

I am confused why you are using two fields to track an update count, however.  Why not just one field that gets incremented after every update?  Otherwise, another way is to turn on Field History tracking and track updates that way.

In the future, when you post to the boards, please use the code formatting button (< >) and format your code so that we can read it easily, but also copy and paste if need be.  Thanks!
 

 
KS KumaarKS Kumaar

wait for few minutes. i will check it and will concren after done it.

Thank you

KS KumaarKS Kumaar
Hi Tintu Babu 4,
I tried with this one.
(<trigger EditsCount on Account (before Update) {
    for(Account ff : Trigger.New){
        if(ff.Edit_Counting__c == null){
            ff.Edit_Counting__c = 0;
        }
        ff.Edit_Counting__c = ff.Edit_Counting__c+1;
    }

}>)

But its working for single record and not woking with bulk records. i added List also, even it is not working with bulk records.

please check the code which i have tried.

(<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 ff : mm){
        RecusrssionTrigger.flag=false;
        if(ff.Counting__c == null){
            ff.Counting__c = 0;
        }
        ff.Counting__c = ff.Counting__c+1;
         mmp.add(ff);
    }
    }
    update mmp;
        

}>)
TintuBabuTintuBabu
Hi
I just tested in developer console the trigger i have given you

List<Account> nn = [SELECT id,name,fax,QNS__EditsCount__c FROM Account WHERE lastmodifieddate = TODAY];
for(Account bb:nn){
  bb.fax = '1234';
}
update nn;
List<Account> nn1 = [SELECT id,name,fax,QNS__EditsCount__c FROM Account WHERE lastmodifieddate = TODAY];

system.debug(nn1);

and it works
 
TintuBabuTintuBabu
ignore the names of field i have some other fields in ma org for testing like fax insted of country__c
KS KumaarKS Kumaar
@Tintu Babu,
If a use lastmodifieddate=today, i can get records which modified today. So, finally it seems to like unable to handle prevoius day records ?