You need to sign in to do that
Don't have an account?

Count number of opportunities made in my org today
I currently have a workflow setup to, on opportunity creation, set the opportunity name via Workflow to update field name to YYYY-MM-DDXX, where 'XX' is the number of opp that has been created on that date within the entire org.
For example:
- If I made an opp right now, and it was the first opp created today, it would be named 2017-08-2201
- If I created another opp right after, the opp name would be 2017-08-2202
- ...and so on
- But, when the clock strikes midnight, the last two numbers would reset, so the first opp created after midnight would be 2017-08-2301
The way I have this workflow setup is through an autonumber field, as such: {YYYY}-{MM}-{DD}{00}. The problem I've run into is that the {00} (in the XX in my first example) does not reset daily so, I have to manually change it to a text field, then back to an autonumber field every morning.
I have never written APEX but, I’ve been trying to give it a shot. From what I understand, I have to have a ‘count field’ on the opportunity to keep track of the number made that day. Is this correct?
Either way, below is the code I have thus far --
OppCount = the field I created for the above purpose.
public class incrementHandler
{
public void onInsert( list<Opportunity> newList){
List<Opportunity> lstOpp = [SELECT Id,OppCount FROM Opportunity Order BY Createddate DESC LIMIT 1];
Integer intCounter = lstOpp.size() != 0 ? lstOpp[0].OppCount : 0;
for(Opportunity objOpportunity: newList)
{
intCounter ++;
objOpportunity.OppCount = intCounter;
}
}
}
Any help is much appreciated!
Best!
For example:
- If I made an opp right now, and it was the first opp created today, it would be named 2017-08-2201
- If I created another opp right after, the opp name would be 2017-08-2202
- ...and so on
- But, when the clock strikes midnight, the last two numbers would reset, so the first opp created after midnight would be 2017-08-2301
The way I have this workflow setup is through an autonumber field, as such: {YYYY}-{MM}-{DD}{00}. The problem I've run into is that the {00} (in the XX in my first example) does not reset daily so, I have to manually change it to a text field, then back to an autonumber field every morning.
I have never written APEX but, I’ve been trying to give it a shot. From what I understand, I have to have a ‘count field’ on the opportunity to keep track of the number made that day. Is this correct?
Either way, below is the code I have thus far --
OppCount = the field I created for the above purpose.
public class incrementHandler
{
public void onInsert( list<Opportunity> newList){
List<Opportunity> lstOpp = [SELECT Id,OppCount FROM Opportunity Order BY Createddate DESC LIMIT 1];
Integer intCounter = lstOpp.size() != 0 ? lstOpp[0].OppCount : 0;
for(Opportunity objOpportunity: newList)
{
intCounter ++;
objOpportunity.OppCount = intCounter;
}
}
}
Any help is much appreciated!
Best!
All Answers
Please try above code and let me know if something doesn't work.
When trying to add the APEX class I received the following error:
Now, I added the trigger. How do I test then deploy to the active org?
THANK YOU!