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

Help with APEX trigger test unit
Howdy!
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. Via dev forums, I was able to generate a correct class and class test. But, I have not been able to make a successful test trigger.
Below is the code I have thus far --
OppCount = the field I created for the above purpose.
_____
Class -------
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 : Integer.valueOf(lstOpp[0].OppCount__c);
for(Opportunity objOpportunity: newList)
{
intCounter ++;
objOpportunity.OppCount__c = intCounter;
}
}
}
Trigger -------
trigger OpportunityTrigger on Opportunity(before insert)
{
incrementHandler objHandler = new incrementHandler();
if(Trigger.isBefore && Trigger.isInsert)
{
objHandler.onBeforeInsert(Trigger.New);
}
}
Test Class -------
@isTest
private class incrementHandlerTest
{
static testMethod void testOnBeforeInsert()
{
Opportunity one = new Opportunity();
Opportunity two = new Opportunity();
list<Opportunity> newList = new list<Opportunity>();
newList.add(one);
newList.add(two);
new incrementHandler().onBeforeInsert(newList);
Integer diff = Integer.valueOf(two.OppCount__c) - Integer.valueOf(one.OppCount__c);
System.assertEquals(diff, 1, 'expected Opportunity OppCount to differ by 1');
}
}
Any help is appreciated! :)
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. Via dev forums, I was able to generate a correct class and class test. But, I have not been able to make a successful test trigger.
Below is the code I have thus far --
OppCount = the field I created for the above purpose.
_____
Class -------
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 : Integer.valueOf(lstOpp[0].OppCount__c);
for(Opportunity objOpportunity: newList)
{
intCounter ++;
objOpportunity.OppCount__c = intCounter;
}
}
}
Trigger -------
trigger OpportunityTrigger on Opportunity(before insert)
{
incrementHandler objHandler = new incrementHandler();
if(Trigger.isBefore && Trigger.isInsert)
{
objHandler.onBeforeInsert(Trigger.New);
}
}
Test Class -------
@isTest
private class incrementHandlerTest
{
static testMethod void testOnBeforeInsert()
{
Opportunity one = new Opportunity();
Opportunity two = new Opportunity();
list<Opportunity> newList = new list<Opportunity>();
newList.add(one);
newList.add(two);
new incrementHandler().onBeforeInsert(newList);
Integer diff = Integer.valueOf(two.OppCount__c) - Integer.valueOf(one.OppCount__c);
System.assertEquals(diff, 1, 'expected Opportunity OppCount to differ by 1');
}
}
Any help is appreciated! :)
Do you just need a test class for your trigger or something else is also not working fine? Please clarify.
If you need test class for your trigger then below is the code:
Thanks,
Abhishek Bansal.
I was able to get the test class working. But, I just need a test class for my trigger. I used the code you provided (thank you) and I'm getting an error:
Please advise.
I changed line 3 to "static testMethod void incrementHandlerTest()" and the test passed EXCEPT for an error of not providing required fields on the opp.
Thank you!
Theere might be some required fields in your Opportunity object. You have to add some values in those fields in order to run your test classes.
Please let me know if you need any further help on this.
Thanks,
Abhishek Bansal
Is there possibly something wrong with the class?
Hi Sarah,
Can you please let me know what is the actual error that you are facing. If it is related to the required fields then there must be some custom fields defined in your org that have been matked as Required and in the error message you must see their names. Please let me know about the error.
Thanks,
Abhishek Bansal.