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 this trigger.

Hello All,

I need some help in writing a Test Class for the below Trigger.

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;
                bl.Mobile__c = bl.Mobile__c;
                bl.Alternate_Mobile__c= bl.Alternate_Mobile__c;
                bl.Phone__c= bl.Phone__c;
                bl.Home_Phone__c= bl.Home_Phone__c;
                bl.Other_Phone__c= bl.Other_Phone__c;
            }
        }
    }  
}
********************************************************************************

Please Help me with this requirement.

Thanks and Regards,
Azar Khasim.
Rounak SharmaRounak Sharma
hello azar,
your test class will be like this:
@isTest
public class AccountNameUpdate {
static void updateAccount(){
Account a = new Account();
a.Name = 'Test';
a.phone = 12345;
a.Email = 'abc@xyz.com';
a.Customer_Number__pc = ''   //put anything sutable 
insert a;
Booking_Link__c bl= new Booking_Link__c();
bl.T2G__c =True;
bl.Account__c = a.Id;
bl.OwnerId = a.OwnerId;
bl.Mobile__c = bl.Mobile__c;     // After this line you can put the values as you wish.
bl.Alternate_Mobile__c= bl.Alternate_Mobile__c;
bl.Phone__c= bl.Phone__c;
bl.Home_Phone__c= bl.Home_Phone__c;
bl.Other_Phone__c= bl.Other_Phone__c;
update bl;
  }
}

Please let me know if this helps in any way.
thanks
azar khasimazar khasim
Hello Rounak,

I have used the above test class but getting errors in it.

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

Test Class changed as per requirement:
**************************************************************************************************
@isTest
public class AccountNameUpdate {
static void updateAccount(){
Account a = new Account();
a.Name = 'Abhishek Gupta';
  a.phone = 9502146987;
  a.Customer_Number__pc = '006690'   //put anything sutable 
  insert a;

Booking_Link__c bl= new Booking_Link__c();
bl.T2G__c =True;
bl.Account__c = a.Id;
bl.OwnerId = a.OwnerId;
update bl;
  }
}

Errors: In Code with Bold and in Underline fromat.

Illegal Integer
Field is not Writable : a.Customer_Number__pc
Unexpected token : Integer

Help me out from this...
My trigger is also changed now.

Thanks and Regards,
Azar Khasim. 


 
Rounak SharmaRounak Sharma
hello Azar ,

is the field name is Customer_Number__pc or Customer_Number__c?
If it is not writeable then try bl.Customer_Number__c == a.Customer_Number__c. this line you need to add after Booking_Link__c .

Thanks
azar khasimazar khasim
Hello Rounak,

Actually, it is PersonAccount Field as Customer_Number__pc is Correct.

And I was facing with an error as

Error: Expression cannot be a statement.
for  bl.Customer_Number__c == a.Customer_Number__pc;

Help me out from this.

Thanks and Regards,
Azar

 
Rounak SharmaRounak Sharma
hello azim,
bl.Customer_Number__c = a.Customer_Number__pc;
you have to put like this.
azar khasimazar khasim
Hello Rounak,

It was saying add test method in the test class.

Regards,
Azar
 
Rounak SharmaRounak Sharma

hello azar,
you need to add @isTest before the method name too.
Please refer the below link for any other query.
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_triggers

Please mark it as the best answer if it helped you.
Thanks

azar khasimazar khasim
Hello Rounak,

Ya, thanks. But I am getting 0% code Courage on the test class

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

}

Trigger For above Test Class

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

Help me out from this.

Thanks and Regards,
Azar Khasim.