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
dany__dany__ 

hi guys can you guys suggest me the proper way of writing the test class for this trigger

trigger beins_duplicate on Account (after insert) {
    
    account na = trigger.new[0];
  
  contact c=new contact();
  c.lastname=na.name;
  c.phone=na.phone;
  c.accountid=na.id;
  insert c;
  
  opportunity o=new opportunity();
  o.Name = na.Name;
        
        o.CloseDate = date.today();
        o.StageName = 'Prospecting';
  o.accountid=na.id;
  insert o;
}

nd i had written the test class as 

------------------
@istest
public class benis_duplicatetest
{
    static testmethod void test1(){

 account ac=new account(name='hari', phone='9700956194');
/* ac.name='hari';
 ac.phone='9700956194';
  
*/
 insert ac;

 contact co =new contact();
 
 co.accountId=ac.Id;
 co.lastname=ac.name;
 co.phone=ac.phone;
 insert co;
 
system.AssertEquals('hari',co.lastname);
system.AssertEquals('9700956194',co.phone);

}
}
Best Answer chosen by dany__
Sforce.NinjaSforce.Ninja
The trigger is creating contact and Opportunity, this is how you need to create the test class

 
@istest
public class benis_duplicatetest
{
    static testmethod void test1(){

 account ac=new account(name='hari', phone='9700956194');
 ac.name='hari';
 ac.phone='9700956194';
  
 insert ac;

List<Contact> coList=[Select id, name, phone from Contact co where accountid=:ac.id]

//Assuming you are not inserting any other contact

for(Contact co:CoList){
system.AssertEquals('hari',co.lastname);
system.AssertEquals('9700956194',co.phone);
}
//Similarly get your opportunity and check is close date is today.
}
}

P.s. Did not compile this but should work. Let me know if there are any compile errors.

Mark as answered if works.

Cheers,
Siddhesh Kabe
Sforce.Ninja

All Answers

Sforce.NinjaSforce.Ninja
The trigger is creating contact and Opportunity, this is how you need to create the test class

 
@istest
public class benis_duplicatetest
{
    static testmethod void test1(){

 account ac=new account(name='hari', phone='9700956194');
 ac.name='hari';
 ac.phone='9700956194';
  
 insert ac;

List<Contact> coList=[Select id, name, phone from Contact co where accountid=:ac.id]

//Assuming you are not inserting any other contact

for(Contact co:CoList){
system.AssertEquals('hari',co.lastname);
system.AssertEquals('9700956194',co.phone);
}
//Similarly get your opportunity and check is close date is today.
}
}

P.s. Did not compile this but should work. Let me know if there are any compile errors.

Mark as answered if works.

Cheers,
Siddhesh Kabe
Sforce.Ninja
This was selected as the best answer
dany__dany__
thnx Sforce.Ninja
Sforce.NinjaSforce.Ninja
Yo.