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
GaneeeshGaneeesh 

Need test class for this trigger

Can anybody help me i need test class for this trigger..

 

trigger testopp1 on Opportunity (before insert,before update) {

for(Opportunity opp : Trigger.new){

System.debug('****************************'+opp.recordtypeid);

String rectype=[select id,name from RecordType where name =: 'Rec Type1'].id;

System.debug('********convert id into sting************'+rectype);

if(opp.StageName =='Prospecting' && opp.Probability == 10 && opp.recordtypeid == rectype){

if(opp.Amount == null){

opp.Amount.addError('****enter amount*****');

}

}

}

}

Subramani_SFDCSubramani_SFDC

@isTest (SeeAllData = true)
public class testopp1Test {

static testMethod void testopp1 () {


Opportunity opp1 = new Opportunity();
opp1.name = 'Test 1';
opp1.RecordType = [SELECT Id,Name FROM RecordType WHERE SObjectType = 'Opportunity' and Name = 'Rec Type1'];
opp1.Type = 'New Customer';
opp1.Amount = 10;
opp1.CloseDate = system.today();
opp1.StageName = 'Received';
Opp1.Amount=100;

INSERT opp1;

try
{
opp1.Amount=null;

update opp1;
//throw new MyException('An exception should have been thrown by the trigger but was not.'); // 1. If we get to this line it means an error was not added and the test class should throw an exception here. 2. MyException class extends Exception.
}
catch(Exception e)
{
Boolean expectedExceptionThrown = e.getMessage().contains('My Error Message') ? true : false;
System.AssertEquals(expectedExceptionThrown, true);
}


}
}

GlynAGlynA

Here is an alternative solution:

 

    public static testMethod void testTrigger()
    {
        Id  rtid_Opportunity_Rec_Type1 = [SELECT Id FROM RecordType WHERE DeveloperName = 'Rec_Type1'][0].Id;

        Opportunity theOpportunity = new Opportunity
        (   Name            = 'Test Opportunity',
            CloseDate       = Date.today().addMonths( 3 ),
            RecordTypeId    = rtid_Opportunity_Rec_Type1,
            StageName       = 'Prospecting',
            Probability     = 10,
            Amount          = 0
        );
        Database.SaveResult[] list_results = Database.insert( theOpportunity );

        for ( Database.SaveResult result : list_results )
        {
            if ( result.isSuccess() ) continue;

            for ( Database.Error anError : result.getErrors() )
            {
                System.assertEquals( anError.getMessage(), '****enter amount*****' );
            }
        }
    }

I have also refactored the original trigger to move the SOQL query out of the for loop:

 

trigger testopp1 on Opportunity ( before insert, before update )
{
    Id  rtid_Opportunity_Rec_Type1 = [SELECT Id FROM RecordType WHERE DeveloperName = 'Rec_Type1'][0].Id;

    for ( Opportunity opp : Trigger.new )
    {
        if  (   opp.RecordTypeId    == rtid_Opportunity_Rec_Type1
            &&  opp.StageName       == 'Prospecting'
            &&  opp.Probability     == 10
            &&  opp.Amount          == null
            )
        {
            opp.Amount.addError('****enter amount*****');
        }
    }
}

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

GaneeeshGaneeesh

Hi Glyn it shows an Error.....

GlynAGlynA

Tell me the error message, including line number, please.