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
azar khasimazar khasim 

Need help in writing a Test class for a Trigger.

Hello Guys,

I need help in writing a test class for the below trigger.

trigger AccountNameUpdate on Booking_Link__c (before insert, before update) {
    List<Account> accountList = new List<Account>();
    String CustomNumber;
    for(Booking_Link__c bl : Trigger.new)
    {       
        CustomNumber = bl.Customer_Number__c;
       
    }
    accountList = [Select Id, OwnerId, Customer_Number__pc from Account where Customer_Number__pc =:CustomNumber ];
    for(Booking_Link__c bl : Trigger.new)
    {
        for(Account a : accountList)
        {
           if(bl.Customer_Number__c == a.Customer_Number__pc && bl.T2G__c==True)
            {
                bl.Account__c = a.Id;
                bl.OwnerId = a.OwnerId;
                  
            }
        }
    }  
}

*********************************************************************
My test Class which got 0% code coverage.
***********************************************************************
@isTest
public class AccountNameUpdate {  
  @isTest  static void updateAccount(){
        Account a = new Account();
        a.Name = 'Abhishek Gupta';
        insert a;
        Booking_Link__c bl= new Booking_Link__c();
        bl.Customer_Number__c = a.Customer_Number__pc;
        bl.T2G__c =True;
        bl.Account__c = a.Id;
        bl.OwnerId = a.OwnerId;
        update bl;
    }

}

*************************************************
Help me out from this...
Azar Khasim.
Best Answer chosen by azar khasim
a sangeetha 5a sangeetha 5
Hi Azar,

Please find below the test class and the coverage is 100% with all test methods passing. 

@isTest
public class AccountNameUpdate {
@isTest  static void updateAccount(){
        Account a = new Account();
        a.Name = 'Abhishek Gupta';
        insert a;
        Booking_Link__c bl= new Booking_Link__c();
        bl.Customer_Number__c = a.Customer_Number__c;
        bl.T2G__c =True;
        bl.Account__c = a.Id;
        //bl.OwnerId = a.OwnerId;
        insert bl;
    }
}



User-added image

Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue.

Thanks,
Sangeetha A

All Answers

a sangeetha 5a sangeetha 5
Hi Azar,

Please find below the test class and the coverage is 100% with all test methods passing. 

@isTest
public class AccountNameUpdate {
@isTest  static void updateAccount(){
        Account a = new Account();
        a.Name = 'Abhishek Gupta';
        insert a;
        Booking_Link__c bl= new Booking_Link__c();
        bl.Customer_Number__c = a.Customer_Number__c;
        bl.T2G__c =True;
        bl.Account__c = a.Id;
        //bl.OwnerId = a.OwnerId;
        insert bl;
    }
}



User-added image

Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue.

Thanks,
Sangeetha A
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Azar,

First, you have inserted the Booking_Link__c object after that you can update Booking_Link__c object fields.
 
@isTest
public class AccountNameUpdate {  
  @isTest  static void updateAccount(){
        Account a = new Account();
        a.Name = 'Abhishek Gupta';
        a.Customer_Number__pc =  
        insert a;
        
        Booking_Link__c bl= new Booking_Link__c();
        bl.Customer_Number__c = a.Customer_Number__pc;
        bl.T2G__c =True;
        bl.Account__c = a.Id;
        insert b1;
        
        bl.T2G__c = false;
        update b1;
    }

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com