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
krish123krish123 

Test class for Trigger

Q.I am new to salesforce test class.Can you please write test class for the below trigger code.

Thanks in advance !

trigger UpdateItselfAccount on Account (After update)
{
    if(TriggerHelper.isAccountTriggersAreRunning == false){
    List<Account> parentAccounts = new List<Account>();
    
    for(Account currAcc : Trigger.New)
    {
        if(currAcc.parentId != null)
        {
            Account acc = new Account();
            acc.Id = currAcc.parentId;
            acc.Rating = currAcc.Rating;
            parentAccounts.add(acc);
        }
    }
    
    if(parentAccounts.size() > 0)
    update parentAccounts;
 }
}
Pankaj_GanwaniPankaj_Ganwani
@isTest
private class CheckForFunctionality
{
     private testMethod void checkForUpdate()
     {
          Account objParentAccount = new Account(Name = 'Parent');
          insert objParentAccount;
          
         Account objChildAccount = new Account(Name = 'Child', ParentId  = objParentAccount.Id, Rating = 3);
         insert objChildAccount;

        objChildAccount.Rating = 4;
        update objChildAccount;
     }
}

Please try with this code.