You need to sign in to do that
Don't have an account?
Caisa
Testing
Hello,
I am brand new to Apex Testing and have a deadline this week. Can someone please tell me how to write the Apex Test for this:
trigger TruBidCompletion on Opportunity (before update)
{
public static Boolean IsWeekendDay(Date dateParam)
{
boolean result = false;
system.debug('dateParam = '+dateParam);
//Recover the day of the week
Date startOfWeek = dateParam.toStartOfWeek();
system.debug('startOfWeek = '+startOfWeek);
Integer dayOfWeek = dateParam.day() - startOfWeek.day();
system.debug('dayOfWeek = '+dayOfWeek);
result = dayOfWeek == 0 || dayOfWeek == 6 ? true : false;
system.debug('result = '+result);
return result;
}
public static Date AddBusinessDays(Date StartDate, integer BusinessDaysToAdd )
{
//Add or decrease in BusinessDaysToAdd days
Date finalDate = StartDate;
system.debug('finaldate = '+finalDate);
integer direction = BusinessDaysToAdd < 0 ? -1 : 1;
system.debug('direction = '+direction);
while(BusinessDaysToAdd != 0)
{
finalDate = finalDate.AddDays(direction);
system.debug('BusinessDaysToAdd = '+BusinessDaysToAdd);
system.debug('finaldate = '+finalDate);
if (!isWeekendDay(finalDate))
{
BusinessDaysToAdd -= direction;
}
}
return finalDate;
}
{
for(Opportunity o : Trigger.new)
{
/* If the user selects Round 1 as the Number of TruBid Rounds */
if(o.Number_of_Rounds_in_TruBid__c=='Round 1')
{
/* Call the AddBusinessDays method called above to add 'x' days */
o.Contract_Draft_Requested_Start_Date__c=o.Rd_1_Results_Review_End_Date__c;
o.Contract_Draft_Requested_End_Date__c=AddBusinessDays( o.Contract_Draft_Requested_Start_Date__c,1);
o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
}
else if
/* If the user selects Round 2 as the Number of TruBid Rounds */
(o.Number_of_Rounds_in_TruBid__c=='Round 2')
{
/* Call the AddBusinessDays method called above to add 'x' days */
o.Contract_Draft_Requested_Start_Date__c=o.Rd_2_Results_Review_End_Date__c;
o.Contract_Draft_Requested_End_Date__c=AddBusinessDays(o.Contract_Draft_Requested_Start_Date__c,1);
o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
}
else if
/* If the user selects Round 3 as the Number of TruBid Rounds */
(o.Number_of_Rounds_in_TruBid__c=='Round 3')
{
/* Call the AddBusinessDays method called above to add 'x' days */
o.Contract_Draft_Requested_Start_Date__c=o.Rd_3_Results_Review_End_Date__c;
o.Contract_Draft_Requested_End_Date__c=AddBusinessDays(o.Contract_Draft_Requested_Start_Date__c,1);
o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
}
}
}
I am brand new to Apex Testing and have a deadline this week. Can someone please tell me how to write the Apex Test for this:
trigger TruBidCompletion on Opportunity (before update)
{
public static Boolean IsWeekendDay(Date dateParam)
{
boolean result = false;
system.debug('dateParam = '+dateParam);
//Recover the day of the week
Date startOfWeek = dateParam.toStartOfWeek();
system.debug('startOfWeek = '+startOfWeek);
Integer dayOfWeek = dateParam.day() - startOfWeek.day();
system.debug('dayOfWeek = '+dayOfWeek);
result = dayOfWeek == 0 || dayOfWeek == 6 ? true : false;
system.debug('result = '+result);
return result;
}
public static Date AddBusinessDays(Date StartDate, integer BusinessDaysToAdd )
{
//Add or decrease in BusinessDaysToAdd days
Date finalDate = StartDate;
system.debug('finaldate = '+finalDate);
integer direction = BusinessDaysToAdd < 0 ? -1 : 1;
system.debug('direction = '+direction);
while(BusinessDaysToAdd != 0)
{
finalDate = finalDate.AddDays(direction);
system.debug('BusinessDaysToAdd = '+BusinessDaysToAdd);
system.debug('finaldate = '+finalDate);
if (!isWeekendDay(finalDate))
{
BusinessDaysToAdd -= direction;
}
}
return finalDate;
}
{
for(Opportunity o : Trigger.new)
{
/* If the user selects Round 1 as the Number of TruBid Rounds */
if(o.Number_of_Rounds_in_TruBid__c=='Round 1')
{
/* Call the AddBusinessDays method called above to add 'x' days */
o.Contract_Draft_Requested_Start_Date__c=o.Rd_1_Results_Review_End_Date__c;
o.Contract_Draft_Requested_End_Date__c=AddBusinessDays( o.Contract_Draft_Requested_Start_Date__c,1);
o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
}
else if
/* If the user selects Round 2 as the Number of TruBid Rounds */
(o.Number_of_Rounds_in_TruBid__c=='Round 2')
{
/* Call the AddBusinessDays method called above to add 'x' days */
o.Contract_Draft_Requested_Start_Date__c=o.Rd_2_Results_Review_End_Date__c;
o.Contract_Draft_Requested_End_Date__c=AddBusinessDays(o.Contract_Draft_Requested_Start_Date__c,1);
o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
}
else if
/* If the user selects Round 3 as the Number of TruBid Rounds */
(o.Number_of_Rounds_in_TruBid__c=='Round 3')
{
/* Call the AddBusinessDays method called above to add 'x' days */
o.Contract_Draft_Requested_Start_Date__c=o.Rd_3_Results_Review_End_Date__c;
o.Contract_Draft_Requested_End_Date__c=AddBusinessDays(o.Contract_Draft_Requested_Start_Date__c,1);
o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
}
}
}
All Answers
Please find below Test Class for above trigger.
Let us know if that helps you.
Best Regards,
BALAJI
If possible, share a screenshot with detailed Error/Exception.
Then click on Errors. The Error will popup on screen like below screenshot.
Please copy and paste that error here to improve the test class. Also let me know if there are any other Required fields in your Opportunity object.
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.TruBidCompletion: line 33, column 1
Trigger.TruBidCompletion: line 55, column 1: []
I get the same error when I run the code you suggested:
System.DmlException: Update failed. First exception on row 0 with id 00654000002kw8oAAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TruBidCompletion: execution of BeforeUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.TruBidCompletion: line 33, column 1
Trigger.TruBidCompletion: line 55, column 1: []
Can you please help me de-bug it?
Here is the code which is causing Null Pointer: https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=906F0000000QtwsIAC
What am I doing incorrectly?
/* This Trigger is to calculate the Dates in the TruBid Completion
section based on the Number of TruBid Rounds the User has selected.
I've created a method to calculate the Business Days and called
the method to calculate the Dates */
trigger TruBidCompletion on Opportunity (before update)
{
public static Boolean IsWeekendDay(Date dateParam)
{
boolean result = false;
system.debug('dateParam = '+dateParam);
//Recover the day of the week
Date startOfWeek = dateParam.toStartOfWeek();
system.debug('startOfWeek = '+startOfWeek);
Integer dayOfWeek = dateParam.day() - startOfWeek.day();
system.debug('dayOfWeek = '+dayOfWeek);
result = dayOfWeek == 0 || dayOfWeek == 6 ? true : false;
system.debug('result = '+result);
return result;
}
public static Date AddBusinessDays(Date StartDate, integer BusinessDaysToAdd )
{
//Add or decrease in BusinessDaysToAdd days
Date finalDate = StartDate;
system.debug('finaldate = '+finalDate);
integer direction = BusinessDaysToAdd < 0 ? -1 : 1;
system.debug('direction = '+direction);
while(BusinessDaysToAdd != 0)
{
finalDate = finalDate.AddDays(direction);
system.debug('BusinessDaysToAdd = '+BusinessDaysToAdd);
system.debug('finaldate = '+finalDate);
if (!isWeekendDay(finalDate))
{
BusinessDaysToAdd -= direction;
}
}
return finalDate;
}
{
for(Opportunity o : Trigger.new)
{
/* If the user selects Round 1 as the Number of TruBid Rounds */
if(o.Number_of_Rounds_in_TruBid__c=='Round 1')
{
/* Call the AddBusinessDays method called above to add 'x' days */
o.Contract_Draft_Requested_Start_Date__c=o.Rd_1_Results_Review_End_Date__c;
o.Contract_Draft_Requested_End_Date__c=AddBusinessDays( o.Contract_Draft_Requested_Start_Date__c,1);
o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
}
else if
/* If the user selects Round 2 as the Number of TruBid Rounds */
(o.Number_of_Rounds_in_TruBid__c=='Round 2')
{
/* Call the AddBusinessDays method called above to add 'x' days */
o.Contract_Draft_Requested_Start_Date__c=o.Rd_2_Results_Review_End_Date__c;
o.Contract_Draft_Requested_End_Date__c=AddBusinessDays(o.Contract_Draft_Requested_Start_Date__c,1);
o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
}
else if
/* If the user selects Round 3 as the Number of TruBid Rounds */
(o.Number_of_Rounds_in_TruBid__c=='Round 3')
{
/* Call the AddBusinessDays method called above to add 'x' days */
o.Contract_Draft_Requested_Start_Date__c=o.Rd_3_Results_Review_End_Date__c;
o.Contract_Draft_Requested_End_Date__c=AddBusinessDays(o.Contract_Draft_Requested_Start_Date__c,1);
o.Award_Start_Date__c=o.Contract_Draft_Requested_End_Date__c;
o.Award_End_Date__c=AddBusinessDays(o.Award_Start_Date__c,15);
o.TB_Draft_Received_Start_Date__c= o.Award_End_Date__c;
o.TB_Draft_Received_End_Date__c=o.Award_End_Date__c;
o.TB_Edits_in_Progress_Start_Date__c=o.TB_Draft_Received_End_Date__c;
o.TB_Edits_in_Progress_End_Date__c=AddBusinessDays(o.TB_Edits_in_Progress_Start_Date__c,47);
o.TB_Contract_Signed_Start_Date__c=o.TB_Edits_in_Progress_End_Date__c;
o.TB_Contract_Signed_End_Date__c=AddBusinessDays(o.TB_Contract_Signed_Start_Date__c,1);
}
}
}
}
Test Class
@isTest
public class Test_TruBidCompletion {
public static testmethod void test1()
{
Account acc= new Account();
acc.name ='test';
// add all required field here
insert acc;
Opportunity op = New Opportunity ();
op.name ='TestOpportunity';
op.CloseDate = system.today();
op.StageName = 'Prospecting' ;
op.accountid = acc.id;
op.TB_Edits_in_Progress_Start_Date__c = System.today();
op.TB_Edits_in_Progress_End_Date__c = System.today()+8;
op.Contract_Draft_Requested_Start_Date__c = System.today();
op.Award_Start_Date__c = System.today();
op.TB_Contract_Signed_Start_Date__c = System.today();
insert op;
op.Number_of_Rounds_in_TruBid__c = 'Round 1';
update op;
}
public static testmethod void test2()
{
Account acc= new Account();
acc.name ='test';
// add all required field here
insert acc;
Opportunity op = New Opportunity ();
op.name ='TestOpportunity';
op.CloseDate = system.today();
op.StageName = 'Prospecting' ;
op.accountid = acc.id;
op.TB_Edits_in_Progress_Start_Date__c = System.today();
op.TB_Edits_in_Progress_End_Date__c = System.today()+8;
op.Contract_Draft_Requested_Start_Date__c = System.today();
op.Award_Start_Date__c = System.today();
op.TB_Contract_Signed_Start_Date__c = System.today();
insert op;
op.Number_of_Rounds_in_TruBid__c = 'Round 2';
update op;
}
public static testmethod void test3()
{
Account acc= new Account();
acc.name ='test';
// add all required field here
insert acc;
Opportunity op = New Opportunity ();
op.name ='TestOpportunity';
op.CloseDate = system.today();
op.StageName = 'Prospecting' ;
op.accountid = acc.id;
op.TB_Edits_in_Progress_Start_Date__c = System.today();
op.TB_Edits_in_Progress_End_Date__c = System.today()+8;
op.Contract_Draft_Requested_Start_Date__c = System.today();
op.Award_Start_Date__c = System.today();
op.TB_Contract_Signed_Start_Date__c = System.today();
insert op;
op.Number_of_Rounds_in_TruBid__c = 'Round 3';
update op;
}
}