Trigger with Test class for When Account is created with AnnualRevenue more than 50 Lacs and Industry is Energy then create a new opportunity

By | November 16, 2019

Hi, Here we have witten Trigger for, When ever a new Account is created with Annual Revenue more than 50Lacs and Industry as Energy then create a new opportunity for this account with

OpportunityName as AccountName

StageName as Prospecting

CloseDate as Today+30 days

Trigger Code

trigger AccountInsert on Account (after insert) {
	List<Opportunity> optyList=new List<Opportunity>();
    for(Account a: Trigger.New){
        if(a.industry=='Energy' && a.AnnualRevenue >5000000){
            Opportunity op=new Opportunity();
            op.name=a.name;
            op.stageName='Prospecting';
            op.CloseDate=System.today()+30;
            op.accountId=a.id;
            optyList.add(op);
        }
    }
    insert optyList;
}

Test Class for this Trigger

This is Test class for the above trigger for the reference purpose

@isTest
private class AccountInsertTest {
	@isTest
    static void testme(){
        Account a1=new Account();
        a1.name='Test';
        a1.Industry='Energy';
        a1.AnnualRevenue=9000000;
        try{
            insert a1;
        }catch(Exception e){
            
        }
        Integer count=[select count() from Opportunity];
        if(a1.Industry=='Energy' && a1.AnnualRevenue>5000000)
        	System.assertEquals(count,1);
        else
         	System.assertEquals(count,0);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *