You need to sign in to do that
Don't have an account?

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
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
Hello,
Can you plese post your trigger code for deletion???????????????????
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.');
}
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.
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.
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);
}
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