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
Sarah KalmbachSarah Kalmbach 

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!
 
Best Answer chosen by Sarah Kalmbach
Nayana KNayana K
Integer intCounter = lstOpp.isEmpty() ? 0 : Integer.valueOf(lstOpp[0].OppCount__c);

 

All Answers

Nayana KNayana K
public class incrementHandler
{
    public void onBeforeInsert( list<Opportunity> newList)
	{
		List<Opportunity> lstOpp = [SELECT Id, OppCount__c FROM Opportunity WHERE Createddate = TODAY Order BY OppCount__c DESC LIMIT 1];
		Integer intCounter = lstOpp.isEmpty() ? 0 : lstOpp[0].OppCount__c;
		for(Opportunity objOpportunity: newList)
		{
			intCounter ++;
			objOpportunity.OppCount__c = intCounter;
		}
    }
}
 
trigger OpportunityTrigger on Opportunity(before insert) 
{
	incrementHandler objHandler = new incrementHandler();
	if(Trigger.isBefore && Trigger.isInsert)
	{
		objHandler.onBeforeInsert(Trigger.New);
	}
}

Please try above code and let me know if something doesn't work.
Sarah KalmbachSarah Kalmbach
Hi Nayana, thank you for the response! 

When trying to add the APEX class I received the following error:

User-added image
SalesFORCE_enFORCErSalesFORCE_enFORCEr
What is the data type of OppCount__c ? It should be "Number".
Sarah KalmbachSarah Kalmbach
It was a text field (because I assumed it was fine since you can have numbers and text). Changed it to a number field and now I'm getting this error:

User-added image
Nayana KNayana K
Integer intCounter = lstOpp.isEmpty() ? 0 : Integer.valueOf(lstOpp[0].OppCount__c);

 
This was selected as the best answer
Sarah KalmbachSarah Kalmbach
YES! It allowed me to save! 

Now, I added the trigger. How do I test then deploy to the active org?

THANK YOU!