+ Start a Discussion
Pete Watson 5Pete Watson 5 

TestClass Error ( Required fields are missing: [Name, Body]: [Name, Body]) on Visualforce Attachment Upload Controller Extension

Hi all, 

I have 82% coverage on the below test class but receiving an error on line 133 of the test class: 

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

When I debug the run the name and body fields both apear to have values, not sure where im going wrong? 

Any help would be greatly appreciated. Many thanks in advance
 
@isTest
public class ContractUploadTest {

      @isTest static void ContractUploadTest(){
      
       //Record Types
        Id RetailRecordType = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Retail').getRecordTypeId();
		Id RetailSubAccountRecordType = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Retail Sub-Account').getRecordTypeId();
        
        //Insert Retailer Account
        Account acc = new Account(Name = 'TestRetailer',
                                        Known_As__C = null,
                                  		RecordTypeID = RetailRecordType,
                                        company_Number__c = '00699',
                                        company_Reg_No__c = '12345678',
                                        Phone = '015165321833',
                                        type = 'Customer',
                                        website = 'www.new.com',
                                        BillingCity = 'City',
                                        BillingCountry = 'Country',
                                        BillingPostalCode = 'CH41 7ED',
                                        BillingStreet = 'Street',
                                        Corp_Business_Type__c = 'Staff Rewards- Corporate',
                                        Discount__c = '0%',
                                        Industry = 'Retail',
                                        VAT_Number__c = '000111222',
                                        Retailer_Only_Account__c = true,
                                        Immediate_Set_Up__c = true,
                                        Retail_Sector__c = 'Fashion',
                                        Lead_Source__c= 'Networking',
                                        Payment_Terms__c = '30days',
                                        Service_Fee__c = 10,
                                        Credit_Limit__c = string.valueof(10000),
                                        Sort_Code__c = '112233',
                                        Bank_Account_Number__c = '11223344',
                                        VAT_Email__c = 'test@test.com',
                                        Remittance_Email__c = 'test@test.com',
                                        Day_Working_Week_Ends__c = 'Sunday',
                                        Required_Schemes__c = '12',
                                        Approx_Redemption_s_Per_Month__c = 1000,
                                        Opportunity_Won__c = true,
                                        Sub_Account_Type__c = null,
                                        Account_Stage__c = 'Active',
                                        Partner_Status__c = 'Active',
                                        ActiveInactive_Retailer__c = false);
        
        checkRecursive.resetRunOnceFlag();
        insert acc;
        System.assert(acc.Id !=null);
		system.debug('acc inserted: ' +acc.Name);
        
        //Create Contact
        Contact c1 = new Contact(FirstName = 'Peter',
                                  LastName = 'LastName',
                                  AccountID = acc.id,
                                  email = 'test@new.com',       
                                  Phone = '01516531833');
        insert c1;    
		System.assert(c1.Id !=null);
        acc.Primary_Contact__c = c1.id;
        acc.Financial_Contact__c = c1.id;
          
        update acc;    
         
        //Create Child Account  
        Account childAcc1 = new Account(Name = acc.Name, 
                                        Known_As__c = acc.Name +': Voucher Sub-Account',
                                        RecordTypeID = RetailSubAccountRecordType,
                                        ParentId = acc.id, 
                                        company_Number__c = '00699',
                                        company_Reg_No__c = acc.company_Reg_No__c,
                                        Phone = acc.Phone,
                                        type = 'Customer',
                                        website = acc.website,
                                        BillingCity = acc.BillingCity,
                                        BillingCountry = acc.BillingCountry,
                                        BillingPostalCode = acc.BillingPostalCode,
                                        BillingStreet = acc.BillingStreet,
                                        Corp_Business_Type__c = 'Staff Rewards- Corporate',
                                        Discount__c = '0%',
                                        Industry = 'Retail',
                                        Retail_Sector__c = acc.Retail_Sector__c,
                                        Lead_Source__c= acc.Lead_Source__c,
                                        VAT_Number__c = acc.VAT_Number__c,
                                        Financial_Contact__c = acc.Financial_Contact__c,
                                        Primary_Contact__c = acc.Primary_Contact__c,
                                        Payment_Terms__c = acc.Please_confirm_payment_terms__c,
                                        Service_Fee__c = acc.Please_confirm_service_fee__c,
                                        Credit_Limit__c = string.valueof(acc.Credit_Limit_Required__c),
                                        Retailer_Only_Account__c = true,
                                        Immediate_Set_Up__c = true,
                                        Sort_Code__c = acc.Sort_Code__c,
                                        Bank_Account_Number__c = acc.Bank_Account_Number__c,
                                        VAT_Email__c = acc.VAT_Email__c,
                                        Remittance_Email__c = acc.Remittance_Email__c,
                                        Day_Working_Week_Ends__c = acc.Day_Working_Week_Ends__c,
                                        Required_Schemes__c = acc.Required_Schemes__c,
                                        MerchantID_s__c = acc.MerchantID_s__c,
                                        Sub_Account_Type__c = 'Voucher',
                                        Approx_Redemption_s_Per_Month__c = acc.Approx_Redemption_s_Per_Month__c,
                                        Partner_Status__c = acc.Partner_Status__c);
        
      insert childAcc1;
      System.assert(childAcc1.Id !=null);    
      
      //Create Contract Record    
      Contract__c c = new Contract__c(Name = 'test',
                                       Service_Fee__c = 10,
                                       Type_Of_Contract__c = 'Voucher',
                                       Contract_Term__c = 'fixed',
                                       Start_Date__c = system.today(),
                                       Retailer__c = acc.id,
                                       Invoicing_Account__c = childAcc1.id);
      insert c;
	  System.assert(c.Id !=null);
      
 	  
        ApexPages.currentPage().getParameters().put('id', c.Id);
		ApexPages.StandardController Contract = new ApexPages.StandardController(c);
	    attachmentUpload controller = new attachmentUpload(Contract);
          
        Test.startTest();
          
        Attachment attach = new Attachment();     
                attach.Name='Unit Test Attachment';
                Blob bodyBlob =Blob.valueOf('Unit Test Attachment Body');
                attach.body = bodyBlob;
                attach.parentId = c.id;
          
          insert attach;
          system.debug('******attachment: ' +attach);
        
        controller.getmyfile();
        controller.Savedoc();
          
        Test.stopTest();

      List<Attachment> attachments = [SELECT ID, Name FROM Attachment WHERE parent.id= :c.id];
      System.assertEquals(1, attachments.size());
    }


}

 
Best Answer chosen by Pete Watson 5
Pete Watson 5Pete Watson 5
Figured it out thank you... I needed to assign the attachment values to 'myfile' before the savedoc() method. 

controller.getmyfile();
        controller.myfile.parentId = attach.parentId;
        controller.myfile.Name = attach.name;          
        controller.myfile.body = attach.body; 
controller.Savedoc();

All Answers

SwethaSwetha (Salesforce Developers) 
HI Pete,
Can you highlight what is line 133 in the above code?Thanks
Pete Watson 5Pete Watson 5
Hi Swetha, 

Appologies - its this line: 

controller.Savedoc();

Many thanks 
Pete Watson 5Pete Watson 5
and heres the controller, just in case: 
 
public class attachmentUpload {
	
    public String currentRecordId {get;set;}
    public ID RetailerID {get;set;}
    public Account Retailer {get;set;}
    public ID InvoicingAccID {get;set;}
    public Account InvoicingAcc {get;set;}
    public Contract__c Contract {get;set;}
    
    public attachmentUpload(ApexPages.StandardController controller) {
        currentRecordId  = System.currentPagereference().getParameters().get('id');   
    }

    Public Attachment myfile;
    
    Public Attachment getmyfile(){
        
        myfile = new Attachment();
        return myfile;
        
    }
   
    Public Pagereference Savedoc(){
        
        String Conid = System.currentPagereference().getParameters().get('id');
                system.debug('attachmentsample | currentRecordId: ' + Conid);
        
        Contract = [select id ,name,Retailer__c,Invoicing_Account__c,ContractId__c
               		from Contract__c 
                    where id =: currentRecordId ];
        			system.debug('attachmentsample | Contract: ' + Contract );
        
        RetailerID = Contract.Retailer__c;
        system.debug('attachmentsample | RetailerID: ' + RetailerID );
        InvoicingAccID = Contract.Invoicing_Account__c;
        system.debug('attachmentsample | InvoicingAccID: ' + InvoicingAccID );
        
        
        Retailer = [SELECT id 
                    FROM Account 
                    WHERE id =: RetailerID];
        system.debug('attachmentsample | Retailer: ' + Retailer );
        
        try{
        InvoicingAcc = [SELECT id, ContractId__c, Name 
                        FROM Account 
                        WHERE id =: InvoicingAccID];
        system.debug('attachmentsample | InvoicingAcc: ' + InvoicingAcc );
        }
        catch (Exception e){
         System.debug('attachmentsample | Exception has occurred when listing InvoicingAcc. Type: ' + e.getTypeName() + ' Message: '+ e.getMessage());
         }
        
        
        Attachment a = new Attachment(parentId = Conid, name= myfile.name, body = myfile.body);
         
         /* insert the attachment */
         insert a;
        system.debug('******attachment: ' +a);
        
         //Add to Contract Record
		 Contract.ContractId__c = a.Id;update Contract; 
         system.debug('attachmentsample | Contract Updated: ' + Contract +', with contract id: ' +a.Id);
        
         //Add to Invoicing Account Record
         try{InvoicingAcc.ContractId__c = a.Id;update InvoicingAcc;
         system.debug('attachmentsample | Invoicing Account Updated: ' + InvoicingAcc +', with contract id: ' +a.Id);
         }
         catch (Exception e){System.debug('attachmentsample | Exception has occurred when linking to Sub_Account. Type: ' + e.getTypeName() + ' Message: '+ e.getMessage());
         }
         PageReference ContractPage = new ApexPages.StandardController(Contract).view();ContractPage.setRedirect(true);return ContractPage;
        

}
}

 
Pete Watson 5Pete Watson 5
Figured it out thank you... I needed to assign the attachment values to 'myfile' before the savedoc() method. 

controller.getmyfile();
        controller.myfile.parentId = attach.parentId;
        controller.myfile.Name = attach.name;          
        controller.myfile.body = attach.body; 
controller.Savedoc();
This was selected as the best answer