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
theoptheop 

I need a second pair of eyes (Code Covered: 0%)

This is on the sandbox and I can't figure out why this trigger test isn't firing. All system debug get hit and have the correct data, System.debug('@@@ trgInsertNote');

 

trigger trgInsertNote on Note (after insert) {
    
    System.debug('@@@ trgInsertNote');
    BMCServiceDesk__Incident__c toUpdate = new BMCServiceDesk__Incident__c();
    
    for(Note n : Trigger.New){
        String nName = n.ParentId;
        String iName = Schema.getGlobalDescribe().get('BMCServiceDesk__Incident__c').getDescribe().getKeyPrefix();
        
        if ( nName.startsWith(iName) && !n.IsPrivate ) {
            
            toUpdate = [SELECT X3Ci_Last_Note__c FROM BMCServiceDesk__Incident__c WHERE Id = :nName][0];
            if ( n.Body == '' || n.body == null )
                toUpdate.X3Ci_Last_Note__c = n.Title;
            else
                toUpdate.X3Ci_Last_Note__c = n.Title + ': ' + n.Body;
        }
    }
    
    if ( toUpdate != null)
        update toUpdate;
}

 

@isTest(SeeAllData=true) 
public with sharing class TestTrgInsertNote{
    private static TestMethod void testInsertNoteMethod(){
        
        Profile cvProfile = [select id from profile where name = 'Standard User' limit 1];
        User usrCV = new User();
        usrCV.LastName='Test Client Value';
        usrCV.Username='dsfg@gmail.com23453245';
        usrCV.Alias='tesy07';
        usrCV.Email='sdfg@sdfg.com';
        usrCV.CommunityNickname='Test CV Contact';
        usrCV.TimeZoneSidKey='America/Los_Angeles';
        usrCV.localesidkey='en_US';
        usrCV.EmailEncodingKey='ISO-8859-1';
        usrCV.LanguageLocaleKey ='en_US';
        usrCV.ProfileId=cvProfile.id;
        usrCV.IsActive = true;
        Insert usrCV;
        System.debug('@@@ TestTrgInsertNote.usrCV: ' + usrCV);
        
        BMCServiceDesk__Incident__c testIncident = new BMCServiceDesk__Incident__c();
        testIncident.Subject__c = 'Test Incident';
        testIncident.BMCServiceDesk__FKClient__c = usrCV.Id;
        insert testIncident;
        System.debug('@@@ TestTrgInsertNote.testIncident: ' + testIncident);
        
        Note newNote = new Note(Title='Test title', Body='Test body', ParentId = testIncident.Id, IsPrivate = false);
        System.debug('@@@ TestTrgInsertNote.newNote: ' + newNote);
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
AdrianCCAdrianCC

Hello,

 

Your trigger fires after insert, so you need to add that to your test:

Test.startTest();
insert newNote;
Test.stopTest();

 You are just instantiating a Note object, but not inserting(commiting) it to the db.

 

Happy New Year!

All Answers

SFAdmin5SFAdmin5

What is this trigger supposed to do, exactly?

AdrianCCAdrianCC

Hello,

 

Your trigger fires after insert, so you need to add that to your test:

Test.startTest();
insert newNote;
Test.stopTest();

 You are just instantiating a Note object, but not inserting(commiting) it to the db.

 

Happy New Year!

This was selected as the best answer
theoptheop

Thanks Adrian, that was it. Happy new year to you too, you made mine!

 

@SFAdmin5, the trigger copies the latest Note to a field to an object so users can include it in their reports. You can't use reports, workflows, etc. on note and attachments otherwise.