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
Marco PinderMarco Pinder 

Apex test Method Help Please

I have written the following test method to test a Trigger on the FeedItem object (to prevent deletion through Chatter):

 

@isTest
private class deleteBlockerTest{
        static testMethod void deleteBlockerTest() {
           
            //Set up the User record.
            User u = new User(Firstname='Test',LastName='Test',Username='test.test@test.com.globalsb',Email='test.test@test.com',Alias='ttest',CommunityNickname='test.test',TimeZoneSidKey='GMT',LocaleSidKey='en_GB',EmailEncodingKey='ISO-8859-1',ProfileId='00e50000000yyaB',LanguageLocaleKey='en_US');
            insert u;
           
            //Set up the UserFeed record.
            FeedItem fi = new FeedItem(Body='Test text for Item Body',ParentId=u.Id);
            upsert fi;
                                                        
            //Cause the Trigger to execute.
            delete fi;
           
            //Verify that the results are as expected.
            FeedItem fi2 = [select Id,IsDeleted from FeedItem where id = :fi.Id];
            System.assertEquals(fi2.isdeleted,false);
            }
        }

 

However, I am getting the following error message:

 

System.DmlException: Delete failed. First exception on row 0 with id 0D5R0000002Lkp1KAC; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, deleteBlocker: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.deleteBlocker: line 2, column 20: []

 

Can someone please advise me what I am doing wrong?

 

Thanks,

 

Marco

Best Answer chosen by Admin (Salesforce Developers) 
ashish raiashish rai

Hello,

    Please make the change inside your trigger with trigger.old insted of trigger.new. Like below mention.

trigger deleteBlocker on FeedItem (before delete)

{
for( feedItem fi : trigger.old )
fi.body.addError('You may not delete chatter posts.');

}

 

Think this will help you.

All Answers

ashish raiashish rai

Hello,

    Can you plese post your trigger code for deletion???????????????????

Marco PinderMarco Pinder

Of course, apologies for not including it.

 

The Trigger to block deletion of a FeedItem is:

 

trigger deleteBlocker on FeedItem (before delete) {
for( feedItem fi : trigger.new )
fi.body.addError('You may not delete chatter posts.');
}

ashish raiashish rai

Hello,

    Please make the change inside your trigger with trigger.old insted of trigger.new. Like below mention.

trigger deleteBlocker on FeedItem (before delete)

{
for( feedItem fi : trigger.old )
fi.body.addError('You may not delete chatter posts.');

}

 

Think this will help you.

This was selected as the best answer
Marco PinderMarco Pinder

Hi Ashish,

 

Thanks for your feedback.

 

Can you explain what changing the trigger.old to trigger.new will do please?

 

The trigger itself works well normally, it is just the test method that I am running into trouble with.

ashish raiashish rai

Hello,

    Well if your trigger is working fine then there is no any need to change that , insted just remove the last two statement of your test method. It should be this:

        }

@isTest
private class deleteBlockerTest{
        static testMethod void deleteBlockerTest() {
           
            //Set up the User record.
            User u = new User(Firstname='Test',LastName='Test',Username='test.test@test.com.globalsb',Email='test.test@test.com',Alias='ttest',CommunityNickname='test.test',TimeZoneSidKey='GMT',LocaleSidKey='en_GB',EmailEncodingKey='ISO-8859-1',ProfileId='00e50000000yyaB',LanguageLocaleKey='en_US');
            insert u;
           
            //Set up the UserFeed record.
            FeedItem fi = new FeedItem(Body='Test text for Item Body',ParentId=u.Id);
            upsert fi;
                                                        
            //Cause the Trigger to execute.
            delete fi;
           
            //Verify that the results are as expected.
          //  FeedItem fi2 = [select Id,IsDeleted from FeedItem where id = :fi.Id];
          //  System.assertEquals(fi2.isdeleted,false);
            }

Marco PinderMarco Pinder

Ashish,

 

Your trigger.new to trigger.old was the answer!

 

I didn't realise that the current message I was getting when the trigger was fired was incorrect.

 

All resolved now.

 

Thanks,

 

Marco