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
VaderVader 

Need Help with Test Class for Chatter Feed Insert

I have a trigger that will insert a feed item whenever an attachment is added to a record.  I've tried a few things to get a test class in place but am striking out.  Here is the trigger:

 

trigger attachmentTrigger on Attachment (after delete, after insert, after update) {
	
	if(trigger.isinsert) {
		for(Attachment a:trigger.new){
			//system.debug(a);
			feedItem f= new feedItem();
			f.ParentId=a.ParentId;
			f.Title=a.Name;
			//f.body=a.body;
			f.body=((Trigger.isInsert) ? 'New' : 'Updated') +'Attachment'+ ' '+ a.Name +' ' +'is added ';
			insert f;
			//system.debug('f.id--->'+f.id);
		}
	}	
	
	if(trigger.isdelete) {
		for(Attachment a:trigger.old){
			//system.debug(a);
			feedItem f= new feedItem();
			f.ParentId=a.ParentId;
			f.Title=a.Name;
			//f.body=a.body;
			f.body=((Trigger.isDelete) ? 'Attachment' : 'Deleted') + ' '+ a.Name +' ' +'is deleted ';
			insert f;
			//system.debug('f.id--->'+f.id);
		}
	}
}

 

Any help writing a test class would be hugely appreciated!

Best Answer chosen by Admin (Salesforce Developers) 
amarcuteamarcute
  1. Try the following method. this should work.
  2.  
  3. /** 
  4.  * Create a testing attachment for the opportunity 
  5.  */  
  6. private static void addAttachmentToParent(Id parentId) {  
  7.     Blob b = Blob.valueOf('Test Data');  
  8.       
  9.     Attachment attachment = new Attachment();  
  10.     attachment.ParentId = parentId;  
  11.     attachment.Name = 'Test Attachment for Parent';  
  12.     attachment.Body = b;  
  13.       
  14.     insert(attachment);  
  15. }  

All Answers

souvik9086souvik9086

Try this

 

@isTest
private class testClass_Trigger{
private static TestMethod void JWGetAssociations(){
Account acct1 = new Account(name='TestAccount1');
insert acct1;
Attachment a = new Attachment();
a.ParentId = acct1.id;
a.name = 'Test';
insert a;
delete a;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

VaderVader

Thanks for the help!  I got the following error:

 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Body]: [Body]

 

I added 'a.Body' and set a text value it to meet the required field request and got the following error:

 

Description Resource Path Location Type
Save error: Illegal assignment from String to Blob 

 

 

Not sure how to load a body type of blob so that it will be recognized in the test class.

amarcuteamarcute
  1. Try the following method. this should work.
  2.  
  3. /** 
  4.  * Create a testing attachment for the opportunity 
  5.  */  
  6. private static void addAttachmentToParent(Id parentId) {  
  7.     Blob b = Blob.valueOf('Test Data');  
  8.       
  9.     Attachment attachment = new Attachment();  
  10.     attachment.ParentId = parentId;  
  11.     attachment.Name = 'Test Attachment for Parent';  
  12.     attachment.Body = b;  
  13.       
  14.     insert(attachment);  
  15. }  
This was selected as the best answer
VaderVader

Fantastic!  Worked great.  Thanks to the both of you for helping.  Kudos given!