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
Thulasi ReddysThulasi Reddys 

How to write a trigger to count records based on particular field & storing the count value in another field?

Prashant Pandey07Prashant Pandey07
Hi Thulasi,

Check this example http://developer.salesforce.com/forums/?id=9060G0000005OKkQAM  (http://developer.salesforce.com/forums/?id=9060G0000005OKkQAM )

or let me know if you have a specific situation where you would like to count the record.


--
Thanks,
Prashant
David @ ConfigeroDavid @ Configero
If you are using a master/detail relationship I would first suggest rollup fields
Angela SchloederAngela Schloeder

Oops, that link doesn't exist.
Prashant Pandey07Prashant Pandey07
Hi Angela,

Please use this link..somehow salesforce is adding an extra parameter in the url..

https://developer.salesforce.com/forums/?id=9060G0000005OKkQAM


--
Thanks,
Prashant
Angela SchloederAngela Schloeder
I have the task of counting how many Events per user, per month have a checkbox (Request Meeting Bonus) selected. How can this be done? That number will determine the amount Bonus_MR__c is per user. That part should be easily done with a process or workflow. My main problem is calulating the number of Requests. 
 
Prashant Pandey07Prashant Pandey07
Hi,

You can check this code..Here I'm updated the user Total_Request_Meeting_Bonus__c fields based on number of events which has Request_Meeting_Bonus__c true
Note-  I have not compiled this code
 
trigger Counteventon event(after insert, after update) {
    
    public List<ecent> ltask1 = new List<event>();
    public id userids;
    public integer inp=0;   
    public integer inr=0;
    for(event t:Trigger.New){
        userids = t.WhatId;
      
        
    }
	ltask1 = [select id,Requested_meeting_bonus__c from task where whatid=:oppid];

    
    
    for(event t:ltask1){
        if(t.Requested_meeting_bonus__c==true){
            inp = inp+1;
        } else{
            inr = inr +1;
        }                
    }
    List<user> uopp = new List<user>();	
    List<user> userop = [select id from user where id = :userids];

    for(user u: userop){
        u.Total_Requested_meeting_bonus__c = inp;
    
        uopp.add(u);  
    }
    if(uopp.size()>0){
    	update uopp;
	}        
 
}


--
Thanks,
Prashant
Angela SchloederAngela Schloeder
Error: Compile Error: Variable does not exist: oppid at line 12 column 76 ltask1 = [select id,Requested_meeting_bonus__c from task where whatid=:oppid]; Best, Angie
Prashant Pandey07Prashant Pandey07
Update the line 8 and 12

userids = t.ownerid;

ltask1 = [select id,Requested_meeting_bonus__c from event where ownerid=:userids];

--
Thanks,
Prashant
Angela SchloederAngela Schloeder
OMG!! Success. I have worked on this for so long. You’re the best! Thank you so much for your help. Best, Angie
Prashant Pandey07Prashant Pandey07
Good to here..
Hit like and mark this as the best answer.

--
Thanks,
Prashant
Angela SchloederAngela Schloeder
One more question; how to reset this at a new calendar month?
 
Prashant Pandey07Prashant Pandey07
@Angela,
You can try something like this and update the line 12

Date startOfMonth = Date.today().toStartOfMonth();
Date startOfNextMonth = startOfMonth.addMonths(1);

ltask1 = [select id,Requested_meeting_bonus__c from event where ownerid=:userids where CreatedDate >= :startOfMonth AND                                                                                                                                            CreatedDate < :startOfNextMonth];


--
Thanks,
Prashant
Prashant Pandey07Prashant Pandey07
Hi Angela,

Please close this thrad by marking the best answer..

--
Thanks,
Prashant