+ Start a Discussion
azhar khasimazhar khasim 

Need help in my Test Class which has written for Trigger getting 0% code Courage.

Hello,

I need some help in my test class which has written on Trigger.

My Test Class :

@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;
    }

}


My 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;
                  
            }
        }
    }  
}

Please Help me out from this

Thanks and Regards,
Azar Khasim.
Best Answer chosen by azhar khasim
Neha AggrawalNeha Aggrawal
Hi Azar,

I did this and got 100 code coverage:
 
@isTest
public class AccountNameUpdate {  
  @isTest  static void updateAccount(){
 
        Account a = new Account();
        a.Name = 'Abhishek Gupta';
        a.Customer_Number__pc='A-12345';
        insert a;
        Booking_Link__c bl= new Booking_Link__c();
        bl.Customer_Number__c = a.Customer_Number__pc;
        bl.T2G__c =True;
        Test.startTest();
        insert bl;
        bl.Account__c = a.Id;
        update bl;
       Test.stopTest();
      //assert statements
    }

}

With your original test class, I got the error in update statement. This was because you did not put an insert statement first. Second error I got was with the owner Id update statement. 

Hope this helps.
Thanks and Regards, 
Neha Aggrawal
www.initaura.com - Everything Salesforce (https://www.initaura.com)

All Answers

Rounak SharmaRounak Sharma
hi Azar,
Can you try in this way.
@isTest
private class TestAccountNameUpdate {
    @isTest static void TestUpdateAccountWithOneBookingLink() {
        // Test data setup
        // Create an account with a Booking Link, and then try to update it
        Account acct = new Account(Name='Test Account');
        insert acct;
        Booking_Link__c bl = new Booking_Link__c (Name=acct.Name + ' BookingLink',                                       
                                       lookupField=acct.Id);
        insert bl ;
        
        // Perform test
        Test.startTest();
        Database.UpdateResult result = Database.UpdateResult (acct, false);
        Test.stopTest();
        // Verify 
        // In this case the deletion should have been stopped by the trigger,
        // so verify that we got back an error.
        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() > 0);
      
    }
    
}

Can you try with this one. Data you have to create your self because we are unaware of the field in the Booking_Link__c Object.
Other than this you are just updating the data so similarly you have to insert the one time and update the same field other time.This will be format to be followed.
Please let me know if you still have doubt.

Thanks
Mukesh_SfdcDevMukesh_SfdcDev
Hi Azar,

Try this
@isTest
public class AccountNameUpdate {  
  @isTest  static void updateAccount(){
        Account a = new Account();
        a.Name = 'Abhishek Gupta';
		a.Customer_Number__pc = '123';
        insert a;
        Booking_Link__c bl= new Booking_Link__c();
        bl.Customer_Number__c = '123';
        bl.T2G__c =True;
		insert b1;
		
        update bl;
    }

}

Suggestion:- Trigger is not bulkify please do some changes accordingly.

Thanks
Mukesh Kumar
Neha AggrawalNeha Aggrawal
Hi Azar,

I did this and got 100 code coverage:
 
@isTest
public class AccountNameUpdate {  
  @isTest  static void updateAccount(){
 
        Account a = new Account();
        a.Name = 'Abhishek Gupta';
        a.Customer_Number__pc='A-12345';
        insert a;
        Booking_Link__c bl= new Booking_Link__c();
        bl.Customer_Number__c = a.Customer_Number__pc;
        bl.T2G__c =True;
        Test.startTest();
        insert bl;
        bl.Account__c = a.Id;
        update bl;
       Test.stopTest();
      //assert statements
    }

}

With your original test class, I got the error in update statement. This was because you did not put an insert statement first. Second error I got was with the owner Id update statement. 

Hope this helps.
Thanks and Regards, 
Neha Aggrawal
www.initaura.com - Everything Salesforce (https://www.initaura.com)
This was selected as the best answer
azhar khasimazhar khasim
Hello Neha,

Thanks for the code.
It worked for the bit.
but I got an error Customer_Number__pc is not writable.

Because Customer_Number__pc is an auto number and is not writable.

Please help me out with this.

Thanks and Regards,
Azar Khasim.

 
Neha AggrawalNeha Aggrawal
Hi Azhar,

Remove that line and run the test again. It will not cause any issue.
Thanks.