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
suresh143suresh143 

Need to test class for this one

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

for ( Opportunity opp : Trigger.new )        

{       

  if(opp.name!=null)        

{       

user usr=[select id,name from user where id =: opp.ownerid];

              opp.Owner_field__c=usr.name;

               String CSDRid= [Select Id from RecordType where RecordType.Name='Sales Opportunity-CSD'].Id;

              if(opp.Stagename == 'New Opportunity' && opp.Probability == 10 && opp.RecordTypeId==CSDRid)    

   {      

  if(opp.Amount == null)      

  {           

opp.Amount.addError('****enter something yar****');     

}       

if ( opp.Total_Amount__c == null )    

{            

opp.Total_Amount__c.addError( 'Please enter a value Total Amount.' );       

  }

}

 

Subramani_SFDCSubramani_SFDC

Try This...it gives 60%...

 

@isTest (SeeAllData = true)
public class ValidateAmountValueAndTotalamountTest {

static testMethod void ValidateAmountValueAndTotalamount () {


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

try
{
opp1.Amount=null;
opp1.Total_Amount__c=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);
}


}
}

suresh143suresh143
Hi Thank You very much for giving this one subra
Subramani_SFDCSubramani_SFDC

Welcome Suresh......I

suresh143suresh143
Hi Thanks for giving it will cover only for 31 see that one :
@isTest
public class ValidateAmountValueAndTotalamountTest {

static testMethod void ValidateAmountValueAndTotalamount () {


Opportunity opp1 = new Opportunity();
opp1.name = 'Test 1';
opp1.RecordType = [SELECT Id,Name FROM RecordType WHERE SObjectType = 'Opportunity' and Name = 'Sales Opportunity-CSD'];
opp1.Type = 'New Customer';
opp1.Amount = 10;
opp1.CloseDate = system.today();
opp1.StageName = 'New Opportunity';
opp1.Probability=10;
Opp1.Amount=100;
opp1.Total_Amount__c='125';
INSERT opp1;

Opportunity opp2 = new Opportunity();
opp2.name = 'Test 2';
opp2.RecordType = [SELECT Id,Name FROM RecordType WHERE SObjectType = 'Opportunity' and Name = 'Sales Opportunity-CSD'];
opp2.Type = 'Existing Customer - Downgrade';
opp2.Amount = 200;
opp2.CloseDate = system.today();
opp2.StageName = 'Qualification';
opp1.Probability=10;
Opp2.Amount=200;
opp2.Total_Amount__c='150';
INSERT opp2;
try
{
opp1.Amount=null;
opp1.Total_Amount__c=null;
update opp1;
opp2.Amount=null;
opp2.Total_Amount__c=null;
update opp2;
}
catch(Exception e)
{
Boolean expectedExceptionThrown = e.getMessage().contains('My Error Message') ? true : false;
System.AssertEquals(expectedExceptionThrown, true);
}
}
suresh143suresh143
this is my full trigger code :and see give that test code
trigger ValidateAmountValueAndTotalamount on Opportunity ( before insert, before update )
{
for ( Opportunity opp : Trigger.new )
{
if(opp.name!=null)
{
user usr=[select id,name from user where id =: opp.ownerid];

opp.Owner_field__c=usr.name;

String CSDRid= [Select Id from RecordType where RecordType.Name='Sales Opportunity-CSD'].Id;

if(opp.Stagename == 'New Opportunity' && opp.Probability == 10 && opp.RecordTypeId==CSDRid)
{
if(opp.Amount == null)
{
opp.Amount.addError('****enter something yar****');
}
if ( opp.Total_Amount__c == null )
{
opp.Total_Amount__c.addError( 'Please enter a value Total Amount.' );
}

}

else if ( opp.StageName == 'Qualification' && opp.Probability == 10 && opp.RecordTypeId==CSDRid)
{

if ( opp.Amount == null )
{
opp.Amount.addError( 'Please********** ' );
}
if ( opp.Total_Amount__c == null )
{
opp.Total_Amount__c.addError( 'Please enter a value Total Amount****.' );
}
}

else if ( opp.StageName == 'Value Proposition' && opp.Probability == 50 && opp.RecordTypeId==CSDRid )
{

if ( opp.Amount == null )
{
opp.Amount.addError( 'only stars**********.' );
}
if ( opp.Total_Amount__c == null )
{
opp.Total_Amount__c.addError( 'here also ***********' );
}
}
else if ( opp.StageName == 'Prospecting' && opp.Probability == 10 && opp.RecordTypeId==CSDRid )
{
if(opp.Two_Year_Revenue__c == 0)
{
opp.Two_Year_Revenue__c.addError( 'Please enter a Two_Year_Revenue__c.' );
}
if(opp.customer_Quotation_Date__c == null)
{
opp.customer_Quotation_Date__c.addError( 'Please enter a customer_Quotation_Date__c.' );
}
if ( opp.Amount == null )
{
opp.Amount.addError( '@@@@@@@@@@@@@Please enter a Amount.' );
}
if ( opp.Total_Amount__c == null )
{
opp.Total_Amount__c.addError( '@@@@@@@@@@@@Please enter a value Total Amount.' );
}
}

}
}
}
Subramani_SFDCSubramani_SFDC

It gives 68%...try to include error....... 

 

@isTest
public class ValidateAmountValueAndTotalamountTest {

static testMethod void ValidateAmountValueAndTotalamount () {


Opportunity opp1 = new Opportunity();
opp1.name = 'Test 1';
opp1.RecordType = [SELECT Id,Name FROM RecordType WHERE SObjectType = 'Opportunity' and Name = 'Sales Opportunity-CSD'];
opp1.Type = 'New Customer';
opp1.Amount = 10;
opp1.CloseDate = system.today();
opp1.two_year_revenue__c=2000;
opp1.StageName = 'New Opportunity';
opp1.customer_Quotation_Date__c=system.today();
opp1.Probability=10;
Opp1.Amount=100;
opp1.Total_Amount__c='125';
INSERT opp1;

Opportunity opp2 = new Opportunity();
opp2.name = 'Test 2';
opp2.RecordType = [SELECT Id,Name FROM RecordType WHERE SObjectType = 'Opportunity' and Name = 'Sales Opportunity-CSD'];
opp2.Type = 'Existing Customer - Downgrade';
opp2.Amount = 200;
opp2.CloseDate = system.today();
opp2.two_year_revenue__c=2000;
opp2.StageName = 'Qualification';
opp1.Probability=10;
Opp2.Amount=200;
opp2.customer_Quotation_Date__c=system.today();
opp2.Total_Amount__c='150';
INSERT opp2;
try
{
opp1.Amount=null;
opp1.Total_Amount__c=null;
opp1.StageName = 'Value Proposition';
opp1.customer_Quotation_Date__c=null;
update opp1;
opp2.Amount=null;
opp2.Total_Amount__c=null;
opp2.StageName = 'Prospecting';
opp2.customer_Quotation_Date__c=null;
opp2.two_year_revenue__c=null;
update opp2;
}
catch(Exception e)
{
Boolean expectedExceptionThrown = e.getMessage().contains('My Error Message') ? true : false;
//System.AssertEquals(expectedExceptionThrown, true);
}
}
}

suresh143suresh143
Hi Subra IT will Give only 31 and copy paste try also
Shweta_AgarwalShweta_Agarwal

try this it will cover 100%

 

@isTest
public class ValidateAmountValueAndTotalamountTest {
static testMethod void ValidateAmountValueAndTotalamount () {

RecordType CSDRid= [Select id,name,SobjectType from RecordType where name=:'Sales Opportunity-CSD'];
List<Opportunity> allOppList = new List<Opportunity>();
Opportunity opp1 = new Opportunity();
opp1.name = 'Test 1';
opp1.RecordTypeId = CSDRid.id;
opp1.Type = 'New Customer';
opp1.CloseDate = system.today();
opp1.two_year_revenue__c=2000;
opp1.StageName = 'New Opportunity';
opp1.customer_Quotation_Date__c=system.today();
opp1.Probability=10;
allOppList.add(opp1);
    
Opportunity opp2 = new Opportunity();
opp2.name = 'Test 2';
opp2.RecordTypeId = CSDRid.id;
opp2.Type = 'Existing Customer - Downgrade';
opp2.CloseDate = system.today();
opp2.two_year_revenue__c=2000;
opp2.StageName = 'Qualification';
opp2.Probability=10;
opp2.customer_Quotation_Date__c=system.today();
allOppList.add(opp2);

Opportunity opp3 = new Opportunity();
opp3.name = 'Test 2';
opp3.RecordTypeId = CSDRid.id;
opp3.Type = 'Existing Customer - Downgrade';
opp3.CloseDate = system.today();
opp3.two_year_revenue__c=2000;
opp3.StageName = 'Value Proposition';
opp3.Probability=50;
opp3.customer_Quotation_Date__c=system.today();
allOppList.add(opp3);

Opportunity opp4 = new Opportunity();
opp4.name = 'Test 2';
opp4.RecordTypeId = CSDRid.id;
opp4.Type = 'Existing Customer - Downgrade';
opp4.CloseDate = system.today();
opp4.StageName = 'Prospecting';
opp4.Probability=10;
opp4.two_year_revenue__c=0;
allOppList.add(opp4);
try{
INSERT allOppList;
}
 catch(DmlException ex)
         {
             System.debug('***********************'+ex);
         }             

}
}

suresh143suresh143
Hi swetha so you great i think so and one more is there
write trigger account and opp then once created opp how many number of opp is displyed in account object in number of opps field in acc object
suresh143suresh143
Hi Subra iam an one req once i have created opportunity the number of opps releted accounts count displayed in account object and account having field Number of opportunities write trigger asking pls given that one
suresh143suresh143
Once i have created record in account object and automatically created releted list contact record created how to achive this