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
June PowerJune Power 

Need a test class please please!

I added a trigger on Note with this scenario which works in the sandbox.  I also need a test class.  Can anyone assist?
trigger UpdateOppLastNote on Note (after insert, after update) {


List<Jr_Processing_Tasks__c> oppLstToUpdate=new List<Jr_Processing_Tasks__c>();
    if(Trigger.isInsert){
        for(Note nt : Trigger.new){
            {
                Jr_Processing_Tasks__c opp=new Jr_Processing_Tasks__c(Id=nt.parentId,Latest_Note__c=nt.Body); 
                oppLstToUpdate.add(opp);
            }   
        }
    }if(Trigger.isUpdate){
        for(Note nt : Trigger.new){
            {
                if(nt.Body != Trigger.oldMap.get(nt.Id).Body){
                   Jr_Processing_Tasks__c opp=new Jr_Processing_Tasks__c(Id=nt.parentId,Latest_Note__c=nt.Body); 
                    oppLstToUpdate.add(opp);
                }
            }
        }
    }
    if(!oppLstToUpdate.isEmpty()){
        try{
            update oppLstToUpdate;
        }catch(DmlException de ){
            System.debug(de);
        }
    }


}

 
Maharajan CMaharajan C
Hi June Power,

Please try the below test class:

@isTest
private class ConTest_Test
{
static testMethod void TestAccount()
    {
       Jr_Processing_Tasks__c  Jr = new Jr_Processing_Tasks__c();
      Jr.Name ='Test';
      // add all the other required fields to create Jr_Processing_Tasks__c  Record
      insert Jr;
       
    Note oNote = new Note();     
    oNote.Title = 'Test Note for Parent';
    oNote.Body = 'this is test note'; 
    oNote.parentId = Jr.Id;
    Test.startTest();
    insert oNote; 
    oNote.Body ='This is Test 2 ';
    Update oNote;
​    Test.stopTest();
    }
    }

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
​Raj
 
June PowerJune Power
Thank you!  I renamed the test to UpdateOppLastNote_Test and I received the below error at apex testing.  any ideas? 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateOppLastNote: execution of AfterInsert caused by: System.TypeException: Invalid id value for this SObject type: 001A000001TiVGwIAN Trigger.UpdateOppLastNote: line 8, column 1: [] 
Stack Trace: Class.UpdateOppLastNote_Test.TestAccount: line 14, column 1

 
v varaprasadv varaprasad
Hi June,

Please create test class like following class:
 
@isTest
Public class UpdateOppLastNote_Test{

public static testmethod void testData(){
Jr_Processing_Tasks__c jpt = new Jr_Processing_Tasks__c();
jpt.name = 'TestData'
Insert jpt;

Note nt = new Note();
nt.parentId = jpt;
nt.Body = 'test';
insert nt;
}
}

Hope this helps you.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
v varaprasadv varaprasad
nt.parentId = jpt.id;
June PowerJune Power
ah!
Field is not writeable: Jr_Processing_Tasks__c.Name
 
sfdcMonkey.comsfdcMonkey.com
@isTest
private class ConTest_Test{
static testMethod void UpdateOppLastNote_Test(){
       Jr_Processing_Tasks__c  oJrProcessing_Tasks = new Jr_Processing_Tasks__c();
        oJrProcessing_Tasks.Latest_Note__c ='Test old note';
        // add all the other required fields data here to create Jr_Processing_Tasks__c  Record
      insert oJrProcessing_Tasks;
       
    Note oNote = new Note();     
    oNote.Title = 'test title';
    oNote.Body = 'note body'; 
    oNote.parentId = Jr.Id;
   
   Test.startTest();
      insert oNote; 
    
	oNote.Body ='note body updated ';
      Update oNote;
​    Test.stopTest();
    }
    }
let us know if it helps you
thanks

 
Maharajan CMaharajan C
Just remove this Jr_Processing_Tasks__c.Name Field in test class and add all other fields to insert Jr_Processing_Tasks__c test record in test class. Jr_Processing_Tasks__c Field is writeable manually based on the error u got.

Thanks,
Raj
 
June PowerJune Power
so, my object (Jr_Processing_Tasks__c) has a master-detail relationship to the opportunity (Loan__c).  I cannot figure out how to designate a record in the required Loan field.  I created an opportunity called test1. I tried other test opportunities...

Error:
Class.UpdateOppLastNote_Test.testData: line 8, column 1
@isTest
Public class UpdateOppLastNote_Test{

public static testmethod void testData(){
Jr_Processing_Tasks__c jpt = new Jr_Processing_Tasks__c();
jpt.Status__c = 'Not Started';
jpt.Task__c = 'Payoff';
jpt.Loan__c = 'test1';
Insert jpt;

Note nt = new Note();
nt.parentId = jpt.id;
nt.Body = 'test';
insert nt;
}
}

ugh... I appreciate any help you can give me.  

 
Maharajan CMaharajan C
Hi june,

@isTest
private class ConTest_Test
{
static testMethod void TestAccount()
    {

      Account a = new Account(Name = 'Test Account Joe ');
      insert a;

      Opportunity o = new Opportunity();
      o.AccountId = a.Id;
      o.Name = 'Test_Joe_123';
      o.StageName = 'Prospecting';
      o.CloseDate = date.today();
      o.Type = 'New Business';
       insert o;


       Jr_Processing_Tasks__c  Jr = new Jr_Processing_Tasks__c();
      Jr.Status__c = 'Not Started';
       Jr.Task__c = 'Payoff';
       Jr.Loan__c = 'o.id';    // If its the lookup or Master detail first you have to insert thr test record for that parent Object.
        insert Jr;
       
    Note oNote = new Note();     
    oNote.Title = 'Test Note for Parent';
    oNote.Body = 'this is test note'; 
    oNote.parentId = Jr.Id;
    Test.startTest();
    insert oNote; 
    oNote.Body ='This is Test 2 ';
    Update oNote;
​    Test.stopTest();
    }
    }

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
​Raj