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
Deanna Aaron 11Deanna Aaron 11 

Trigger Missing Test Class

Hi. My trigger failed to deploy. I believe it's missing a test class. I'm unsure how to do this.

This is my trigger:

trigger updateAccountField on Note (after insert) {
    List<Account> accUpdate = new List<Account>();
    for(Note n : Trigger.new){
        if(n.ParentId != null && n.ParentId.getSObjectType() == schema.Account.sObjectType){
Account acc=new Account(Id=n.parentId,Most_Recent_Note_Date__c=n.CreatedDate, Most_Recent_Note_Body__c=n.Body,Most_Recent_Note_Title__c=n.Title);
            accUpdate.add(acc);

       } 
    }
    
    if(accUpdate.size()>0){
        update accUpdate;
    }
}

This is the failed message:
User-added image
Best Answer chosen by Deanna Aaron 11
Steven NsubugaSteven Nsubuga
@isTest
public class updateAccountFieldTest {

    @isTest static void triggerTest() {
		
		Account testAccount = new Account(Name = 'testAccount');
		insert testAccount;
		
        Note testNote = new Note(
            Title = 'Test Note',
			ParentId = testAccount.Id,
            Body = 'testing 1 2 3'
        );
        insert testNote;
		
		Note nt = [SELECT Title, Body, CreatedDate FROM Note LIMIT 1];
		Account acct = [SELECT Most_Recent_Note_Date__c, Most_Recent_Note_Body__c, Most_Recent_Note_Title__c FROM Account LIMIT 1];

		System.assertEquals(acct.Most_Recent_Note_Date__c, nt.CreatedDate);
		System.assertEquals(acct.Most_Recent_Note_Body__c, nt.Body);
		System.assertEquals(acct.Most_Recent_Note_Title__c, nt.Title);
	}	

}

 

All Answers

Steven NsubugaSteven Nsubuga
@isTest
public class updateAccountFieldTest {

    @isTest static void triggerTest() {
		
		Account testAccount = new Account(Name = 'testAccount');
		insert testAccount;
		
        Note testNote = new Note(
            Title = 'Test Note',
			ParentId = testAccount.Id,
            Body = 'testing 1 2 3'
        );
        insert testNote;
		
		Note nt = [SELECT Title, Body, CreatedDate FROM Note LIMIT 1];
		Account acct = [SELECT Most_Recent_Note_Date__c, Most_Recent_Note_Body__c, Most_Recent_Note_Title__c FROM Opportunity LIMIT 1];

		System.assertEquals(acct.Most_Recent_Note_Date__c, nt.CreatedDate);
		System.assertEquals(acct.Most_Recent_Note_Body__c, nt.Body);
		System.assertEquals(acct.Most_Recent_Note_Title__c, nt.Title);
	}	

}

 
Akshay_DhimanAkshay_Dhiman
Hi Deanna,

Try this below test class --
@isTest
public class updateAccountFieldTest {

    @isTest static void triggerTest() {
  
  Account testAccount = new Account(Name = 'testAccount');
  insert testAccount;
  
        Note testNote = new Note(
            Title = 'Test Note',
   ParentId = testAccount.Id,
            Body = 'testing 1 2 3'
        );
        insert testNote;
  
  Account acct = [SELECT Most_Recent_Note_Date__c, Most_Recent_Note_Body__c, Most_Recent_Note_Title__c FROM Account LIMIT 1];

  System.assertEquals(acct.Most_Recent_Note_Date__c, nt.CreatedDate);
  System.assertEquals(acct.Most_Recent_Note_Body__c, nt.Body);
  System.assertEquals(acct.Most_Recent_Note_Title__c, nt.Title);
 } 

}



Thanks 
Akshay
Deanna Aaron 11Deanna Aaron 11
Steven -- Thank you for you reply. I tried inserting the code and received in error.

Error: Compile Error:
SELECT Most_Recent_Note_Date__c, Most_Recent_Note_Body__c
^
ERROR at Row:1:Column:8
No such column 'Most_Recent_Note_Date__c' on entity 'Opportunity'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 17 column 24

See image below:
User-added image
Deanna Aaron 11Deanna Aaron 11
Akshay -- Thank you for your reply. I tried inserting the code and received in error.

Error: Compile Error: Variable does not exist: nt at line 18 column 54

See image below:
User-added image
Deanna Aaron 11Deanna Aaron 11
Thank you for making an adjustment. However, I recieved a new error:

Unable to Access Page
The value of the "id" parameter contains a character that is not allowed or the value exceeds the maximum allowed length. Remove the character from the parameter value or reduce the value length and resubmit. If the error still persists, report it to our Customer Support team. Provide the URL of the page you were requesting as well as any other related information. 
User-added image
Steven NsubugaSteven Nsubuga
@isTest
public class updateAccountFieldTest {

    @isTest static void triggerTest() {
		
		Account testAccount = new Account(Name = 'testAccount');
		insert testAccount;
		
        Note testNote = new Note(
            Title = 'Test Note',
			ParentId = testAccount.Id,
            Body = 'testing 1 2 3'
        );
        insert testNote;
		
		Note nt = [SELECT Title, Body, CreatedDate FROM Note LIMIT 1];
		Account acct = [SELECT Most_Recent_Note_Date__c, Most_Recent_Note_Body__c, Most_Recent_Note_Title__c FROM Account LIMIT 1];

		System.assertEquals(acct.Most_Recent_Note_Date__c, nt.CreatedDate);
		System.assertEquals(acct.Most_Recent_Note_Body__c, nt.Body);
		System.assertEquals(acct.Most_Recent_Note_Title__c, nt.Title);
	}	

}

 
This was selected as the best answer
Deanna Aaron 11Deanna Aaron 11
Steven Nsubuga! You ROCK! That worked. THANK YOU.

User-added image
Steven NsubugaSteven Nsubuga
My pleasure Deanna!!