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
shruthikashruthika 

test class for merging trigger

Hi everyone  am  very new in writing this apex code.. i have following trigger but any how i was able to write two test classes based on condition but was not able to code into single test class.. could anyone please provide me the code how to combine those two test classes into one.

 

Merging MQL Trigger:

trigger MQL on Opportunity (before update) 
{    
for(Opportunity a : Trigger.new)
{        
Opportunity beforeUpdate = System.Trigger.oldMap.get(a.Id);         
if(beforeUpdate.StageName =='Lead'||beforeUpdate.StageName == 'Qualified Lead')
{   
If(a.StageName != 'Lead' && a.StageName != 'Qualified Lead' && a.StageName != 'No Opportunity')
{         
a.MQL_Promotion_Date__c = System.today();            
system.debug('*** Oppty changed from Lead to something other than Lead or No Opportunity - date set to ' + System.today()+' ****');
}
if(a.StageName == 'No Opportunity')
{
a.MQL_Rejection_Date__c = System.today();  
system.debug('*** Oppty changed from Lead to No Opportunity-date set to'+System.today() +'****');  
}                     
}   

}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

@isTest
private class TestMQLPromotionDateTrigger
{
     static testMethod void myTest()
 {
    Datetime t = system.now();
    Date d = Date.newInstance(t.year(),t.month(),t.day());
 
opportunity opp1=new opportunity(Name='MQL',ForecastCategoryName='Pipeline',Region__c='EMEA', LeadSource='Progress Employee', CurrencyIsoCode='INR-Indian Rupee', StageName='Qualified Lead', Type='New Customer', Opportunity_Source__c='sales Rep - Direct', Industry__c='Education',  CloseDate = d);

// insert test opportunity
insert opp1;

// get opportunity and change stage

Opportunity updateOpportunity = [Select Name from Opportunity WHERE Name = 'MQL'];
updateOpportunity.StageName = 'D - Qualify';
update updateOpportunity;

// get updated MQL Promotion Date to verify that trigger populated the field with today's date

Opportunity getOpportunity = [Select MQL_Promotion_Date__c from Opportunity WHERE Name = 'MQL'];

Datetime t2 = system.now();         
Date d2 = Date.newInstance(t2.year(),t2.month(),t2.day());

System.assertEquals(getOpportunity.MQL_Promotion_Date__c,d2);
    }
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@isTest
private class TestMQLPromotionDateTrigger
{
     static testMethod void myTest()
 {

    Datetime t = system.now();
    Date d = Date.newInstance(t.year(),t.month(),t.day());

Opportunity opp1 = new opportunity(Name='MQL',ForecastCategoryName='Pipeline',Region__c='EMEA', LeadSource='Progress Employee', CurrencyIsoCode='INR-Indian Rupee', StageName='Qualified Lead', Type='New Customer', Opportunity_Source__c='sales Rep - Direct', Industry__c='Education',  CloseDate = d);

// insert test opportunity
insert opp1;

// get opportunity and change stage

Opportunity updateOpportunity = [Select Name from Opportunity WHERE Name = 'MQL'];
updateOpportunity.StageName = 'No Opportunity';
updateOpportunity.Stage_Reason__c = 'N - No Solution Fit';
update updateOpportunity;

// get updated MQL Rejection Date to verify that trigger populated the field with today's date

Opportunity getOpportunity = [Select MQL_Rejection_Date__c from Opportunity WHERE Name = 'MQL'];

Datetime t2 = system.now();         
Date d2 = Date.newInstance(t2.year(),t2.month(),t2.day());

System.assertEquals(getOpportunity.MQL_Rejection_Date__c,d2);    
    }
}

can anyone please provide me solution for these two test classes into one with the same condition please..!!

Shashikant SharmaShashikant Sharma

there are two options for you you can write both of your test in a single test method or you can have both of test methods in a test class.

 

Here In this test both test cases are in single test method

 

@isTest
private class TestMQLPromotionDateTrigger
{
     static testMethod void myTest()
 {
    Datetime t = system.now();
    Date d = Date.newInstance(t.year(),t.month(),t.day());
 
opportunity opp1=new opportunity(Name='MQL',ForecastCategoryName='Pipeline',Region__c='EMEA', LeadSource='Progress Employee', CurrencyIsoCode='INR-Indian Rupee', StageName='Qualified Lead', Type='New Customer', Opportunity_Source__c='sales Rep - Direct', Industry__c='Education',  CloseDate = d);

// insert test opportunity
insert opp1;

// get opportunity and change stage

Opportunity updateOpportunity = [Select Name from Opportunity WHERE Name = 'MQL'];
updateOpportunity.StageName = 'D - Qualify';
update updateOpportunity;

// get updated MQL Promotion Date to verify that trigger populated the field with today's date

Opportunity getOpportunity = [Select MQL_Promotion_Date__c from Opportunity WHERE Name = 'MQL'];

Datetime t2 = system.now();         
Date d2 = Date.newInstance(t2.year(),t2.month(),t2.day());

System.assertEquals(getOpportunity.MQL_Promotion_Date__c,d2);

updateOpportunity.StageName = 'Lead';
update updateOpportunity;
        
updateOpportunity.StageName = 'No Opportunity';
update updateOpportunity;

getOpportunity = [Select MQL_Rejection_Date__c from Opportunity WHERE Name = 'MQL'];

t2 = system.now();         
d2 = Date.newInstance(t2.year(),t2.month(),t2.day());
System.assertEquals(getOpportunity.MQL_Rejection_Date__c,d2);

    }
}

 

shruthikashruthika

Thanks for the reply.. but when i run the test it shows an error as following.Please could you let me know this error is because of test class or because of trigger... and the error is as follows

 

line 27, column 54: trigger body is invalid and failed recompilation: Dependent class is invalid and needs recompilation: cs_shareenvironmentprofilehelper: line 81, column 64: sObject type 'Environment_Profile__Share' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.

Shashikant SharmaShashikant Sharma

This error is nothing to do with this testMethod , its because you have changed sharing settings thats why Environment_Profile__Share is not being compiled. Either remove the code of the class which uses this object or change the sharing setting again so that this Environment_Profile__Share become compilable, and save that class or trigger which is using it.