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
Harry1008Harry1008 

Test class for the attachment on Lead apex trigger

HI all,
Could you please help me in writing a test class for the following apex trigger. The trigger is to change the lead status to Negotiation when the user attach the file on to the lead.
Thank you for your help.
trigger UploadFileLead on Attachment (after insert, after update) {
    List <Lead> LeadList = new List<Lead>();
      List <Lead> LeadListToUpdate = new List<Lead>();
    Set <Id> LeadIds = new Set <Id>();
    if(trigger.isInsert || trigger.isUpdate)
    {
        for (Attachment attach : trigger.New) {
            //Check if added attachment is related to Lead or not
            if(attach.ParentId.getSobjectType() == Lead.SobjectType){
                if(string.valueOf(attach.Name).startswith('.xls'))
                    LeadIds.add(attach.ParentId);
                
            }
        }
    }
     
 
LeadList = [select id, status from Lead where id in : LeadIds];
    for(Lead lead : LeadList){
        
        
        lead.Status = 'Negotiation';
        
        
    }
    
    update LeadList;
}

 
Raj VakatiRaj Vakati
try this
@isTest 
public class UploadFileLeadTest {

   static testMethod void testAttachmentsInsert() { 
    
	   Lead newLead = new Lead() ;
        newLead.FirstName = 'Cole';
        newLead.LastName = 'Swain';
        newLead.Company = 'BlueWave';
        newLead.Status = 'Open';
        insert newLead;
      
	
    Attachment attach=new Attachment(); 
    attach.Name='Unit Test Attachment'; 
    Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); 
    attach.body=bodyBlob; 
	attach.parentId=newLead.id;
	attach.ContentType='application/x-excel';
	insert attach;

  

}

static testMethod void testAttachmentsInsertUpdate() { 
    
	   Lead newLead = new Lead() ;
        newLead.FirstName = 'Cole';
        newLead.LastName = 'Swain';
        newLead.Company = 'BlueWave';
        newLead.Status = 'Open';
        insert newLead;
      
	
    Attachment attach=new Attachment(); 
    attach.Name='Unit Test Attachment'; 
    Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); 
    attach.body=bodyBlob; 
	attach.parentId=newLead.id;
	attach.ContentType='application/x-excel';
	insert attach;

       Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body Update'); 
    attach.body=bodyBlob; 
	
	update attach ;
	
}

 
Harry1008Harry1008
@Raj Vakati. Thank you so much for the quick response.
I am getting an error when I run the above test class. This is the error Class.UploadFileLeadTest.testAttachmentsInsertUpdate: line 33, column 1
Raj VakatiRaj Vakati
What is the error you are getting .. i gues sits becasue of the duplicate rule .. please insert lead with some othername and try 
Harry1008Harry1008
@Raj Vakati. Thank you. I am getting the same error if I change the names as well.
Raj VakatiRaj Vakati
what is the error you are getting >>??
Harry1008Harry1008
@Raj vakati. I am getting the following error.
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Venue_Name__c, Channel__c]: [Venue_Name__c, Channel__c]

I have added the values Venue name and channel in my test class
Raj VakatiRaj Vakati
The issue because you have to add the Venue_Name__c and Channel__c field to the lead record insert like below  .. which are required 

Lead newLead = new Lead() ;
newLead.FirstName = 'Cole';
newLead.LastName = 'Swain';
newLead.Company = 'BlueWave';
newLead.Status = 'Open';
newLead.Venue_Name__c ='demo';
newLead.Channel__c ='demo';
//add other required fields 
insert newLead;

 
Harry1008Harry1008
@Raj Vakati. Sorry for chasing you again but I am getting the same error if I insert the values as well.
@isTest
public class UploadFileLeadTest{
    
    static testMethod void testAttachmentInsert() {
        
        Lead newLead = new Lead();
         newLead.FirstName = 'Sam1';
         newLead.LastName = 'Adam';
         newLead.Company = 'Hello World';
         newLead.Venue_Name__c = 'New Hellow World';
         newLead.Channel__c = 'Retail';
         newLead.Status = 'Engagement';
         insert newLead;
        
        
    Attachment attach = new Attachment();
    attach.Name = 'Unit Test Attachment';
    Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
    attach.body = bodyBlob;
    attach.parentId = newLead.id;
    attach.ContentType = 'application/x-excel';
    insert attach;
        
        
        
}
    
    static testMethod void testAttachmentsInsertUpdate() {
        
        Lead newLead = new Lead();
        newLead.Firstname = 'Sam1';
        newLead.LastName = 'Adam';
        newLead.Company = 'Hello World';
        newLead.Venue_Name__c = 'New Hellow World';
        newLead.Channel__c = 'Retail';
        newLead.Status = 'Engagement';
        insert newLead;
       
        
      Attachment attach = new Attachment();
        attach.Name = 'Unit Test Attachment';
        Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
        attach.body = bodyBlob;
        attach.ContentType = 'application/x-excel';
        insert attach;
        
        
         Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body Update');
        attach.body = bodyBlob;
        
        update attach;
    }
}

The error is as per below.
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Venue_Name__c, Channel__c]: [Venue_Name__c, Channel__c]

 
Tad Aalgaard 3Tad Aalgaard 3
There isn't any code in a Trigger or a Process Builder that is blanking them out for some reason?
Harry1008Harry1008
@Tad Aalgaard3. There is no Trigger or a Process Builder that is causing this issue.