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
sreekanth reddysreekanth reddy 

Trigger for Update Count

Hi All,

Please help in the problem I am facing for the below Scenario.
I am working on a trigger to auto count the number of times a filed is updated each month. Though the problem is the filed is on Opportunity object and each User has muiltiple opportunities. So if the field is updated in any opportunity, the field on the User page should increment. This will help us know the number of dispaches each month from each user.

Please help me in approaching the issue.

Regards
Avinash
Ankur Saini 9Ankur Saini 9
Hi Sreekanth,

try this: 
You will have to make 12 fields for each month on user object e.g. "Total_Count_Jun__c".
 
trigger TotalCount on Account (after update) {

    Set<String> userIdSet = new Set<String>();
    List<User> userList = new List<User>();

    for(ID accId:trigger.newMap.keySet()){ 
        if(trigger.newMap.get(accId).Active__c!=trigger.oldMap.get(accId).Active__c){
            userIdSet.add(userinfo.getuserid());
        }
    }

for(User u : [select id,Total_Count_Jan__c,Total_Count_Feb__c,Total_Count_Mar__c,Total_Count_Apr__c,Total_Count_May__c,Total_Count_Jun__c,Total_Count_Jul__c,Total_Count_Aug__c,Total_Count_Sep__c,Total_Count_Oct__c,Total_Count_Nov__c,Total_Count_Dec__c from user where id in : userIdSet]){
    if(date.today().month()==1){
       if(u.Total_Count_Jan__c==null){
            u.Total_Count_Jan__c=+1;
       }
       else{
            u.Total_Count_Jan__c= u.Total_Count_Jan__c+1;    
       }
       userList.add(u);
        }
        
    if(date.today().month()==2){
       if(u.Total_Count_Feb__c==null){
            u.Total_Count_Feb__c=+1;
       }
       else{
            u.Total_Count_Feb__c= u.Total_Count_Feb__c+1;    
       }
       userList.add(u);
        }
        
    if(date.today().month()==3){
       if(u.Total_Count_Mar__c==null){
            u.Total_Count_Mar__c=+1;
       }
       else{
            u.Total_Count_Mar__c= u.Total_Count_Mar__c+1;    
       }
       userList.add(u);
        }
        
    if(date.today().month()==4){
       if(u.Total_Count_Apr__c==null){
            u.Total_Count_Apr__c=+1;
       }
       else{
            u.Total_Count_Apr__c= u.Total_Count_Apr__c+1;    
       }
       userList.add(u);
        }
        
    if(date.today().month()==5){
       if(u.Total_Count_May__c==null){
            u.Total_Count_May__c =+1;
       }
       else{
            u.Total_Count_May__c = u.Total_Count_May__c+1;    
       }
       userList.add(u);
        }
        
    if(date.today().month()==6){
       if(u.Total_Count_Jun__c==null){
            u.Total_Count_Jun__c =+1;
       }
       else{
            u.Total_Count_Jun__c = u.Total_Count_Jun__c+1;    
       }
       userList.add(u);
        }
        
    if(date.today().month()==7){
       if(u.Total_Count_Jul__c==null){
            u.Total_Count_Jul__c =+1;
       }
       else{
            u.Total_Count_Jul__c = u.Total_Count_Jul__c+1;    
       }
       userList.add(u);
        }
        
    if(date.today().month()==8){
       if(u.Total_Count_Aug__c==null){
            u.Total_Count_Aug__c =+1;
       }
       else{
            u.Total_Count_Aug__c = u.Total_Count_Aug__c+1;    
       }
       userList.add(u);
        }
        
    if(date.today().month()==9){
       if(u.Total_Count_Sep__c==null){
            u.Total_Count_Sep__c =+1;
       }
       else{
            u.Total_Count_Sep__c = u.Total_Count_Sep__c+1;    
       }
       userList.add(u);
        }
        
    if(date.today().month()==10){
       if(u.Total_Count_Oct__c==null){
            u.Total_Count_Oct__c =+1;
       }
       else{
            u.Total_Count_Oct__c = u.Total_Count_Oct__c+1;    
       }
       userList.add(u);
        }
        
    if(date.today().month()==11){
       if(u.Total_Count_Nov__c==null){
            u.Total_Count_Nov__c =+1;
       }
       else{
            u.Total_Count_Nov__c = u.Total_Count_Nov__c+1;    
       }
       userList.add(u);
        }
        
    if(date.today().month()==12){
       if(u.Total_Count_Dec__c==null){
            u.Total_Count_Dec__c =+1;
       }
       else{
            u.Total_Count_Dec__c = u.Total_Count_Dec__c+1;    
       }
       userList.add(u);
        }
}
        if(userList.size()>0){
            update userList;
        }
}


Thanks,
Ankur Saini
http://mirketa.com