+ Start a Discussion
deepak kumar 13deepak kumar 13 

Need Help for test for this trigger

hi all,
i need to write test class for this trigger. Please give some idea


trigger accountTestTrggr  on Account (before insert,before update) {
 List<Account> accountsWithContacts = [select id, name,(select id,firstname, lastname,Description from Contacts) from Account where Id IN :Trigger.newMap.keySet()];         
 List<Contact> contactsToUpdate = new List<Contact>{};
    for(Account a: accountsWithContacts){
    for(Contact c: a.Contacts){
    c.Description=c.firstname+c.lastname;
    contactsToUpdate.add(c);
       }
}
update contactsToUpdate;
}
Best Answer chosen by deepak kumar 13
Swati GSwati G
At line 13, you have query in contact and the account doesnot have any contact associated with it. It is best practice to collect query result in a list rather then one single instance.

Your trigger should only run in after update trigger as account will not have any contact when account gets inserted.

All Answers

Swati GSwati G
Refer this, https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
deepak kumar 13deepak kumar 13
hi,
i tried like this below code but getting error as
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, accountTestTrggr: execution of BeforeInsert 
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.accountTestTrggr: line 2, column 1: []

 @isTest
  public class accountTestTrggrTclass{
    static testmethod void accountTestTrggrTclass(){
      Account acc=new Account(Name='TestAcc');
      insert acc;
       
      Contact con=[select LastName,FirstName,Description from Contact where id=:acc.id];
      con.Description ='Lastfirst';
      update con;
      
      contact updatecon=[Select Description from Contact where id=:acc.id];
      system.assertEquals('Lastfirst',updatecon.Description );
     
      }
    }
Swati GSwati G
There is issue in your trigger. Instead of before insert use after insert, so that you can query account.
deepak kumar 13deepak kumar 13
hi, i changed coding like this and changed as after insert but getting
Error as : System.QueryException: List has no rows for assignment to SObject
Class.accountTestTrggrTclass.accountTestTrggrTclass: line 13, column 1

@isTest
  public class accountTestTrggrTclass{
    static testmethod void accountTestTrggrTclass(){
      Account acc=new Account(Name='TestAcc');
      insert acc;
       
      Contact con= new Contact(LastName='New Con');
      insert con;
      
      con.Description ='Lastfirst';
      update con;
      
      contact updatecon=[Select Description from Contact where id=:acc.id];
      system.assertEquals('Lastfirst',updatecon.Description );
     
      }
    }
Swati GSwati G
At line 13, you have query in contact and the account doesnot have any contact associated with it. It is best practice to collect query result in a list rather then one single instance.

Your trigger should only run in after update trigger as account will not have any contact when account gets inserted.
This was selected as the best answer
king kullaiking kullai
hi, your trigger

    direct write the triiger in contact

or
you try to Account write the

after update,after insert  After

 
king kullaiking kullai
hellow boss you write the after insert write the trigger correct code will be convert(present only 71% will covered ) @istest private class Accounttest{ static testmethod void accountTestTrggrTclass(){ Account acc=new Account(); acc.Name='king'; insert acc; update acc; contact con=new contact(); con.Accountid=acc.id; con.Lastname='test'; con.Description='test'; update con; } }