0

Good day please kindly assist me with my test class, I have no idea what I am missing but I am constantly getting the System.DmlException: Insert Failed: REQUIRED_FIELD_MISSING, Required fields are missing: [ParentId]:[ParentId]

Find below the test class, thanks for the help:

   @isTest (seeAllData=true)
   public class CIFController_test 
   {
    static testMethod void CIFControllerTest()
    {
    test.startTest();
        Lead newLead= new Lead
            (
                lastname = 'TestCase',
                company = 'BrianCp',
                Business_Declaration_Date__c = date.today(),
                CEO__c = 'TestCase',
                CFO__c = 'TestCase',
                HCC__c = 'TestCase',
                CEO_Date__c = date.today(),
                CFO_Date__c = date.today(),
                HCC_Date__c = date.today(),
                Registered_Name__c = 'TestCase',
                Date_Of_Registration__c = date.today(),
                Customer_Name__c = 'TestCase',
                Business_Profile__c = 'TestCase',
                Telecommunications_Licences_if_any__c = 'TestCase',
                Surname_and_First_Name__c = 'TestCase',
                Job_Title__c = 'TestCase',
                Telephone_Number__c = 'TestCase',
                Email__c = 'TestCase@mymail.com',
                Trading_As__c = 'TestCase',
                VAT_Number__c = 'TestCase',
                Registration_Number__c = 'TestCase',
                Registered_Address__c = 'TestCase',
                Business_Address__c = 'TestCase',
                Billing_Contact_Telephone_Number__c = 'TestCase',
                Postal_Address__c = 'TestCase',
                Billing_Address_if_not_same_as_above__c = 'TestCase',
                Billing_Contact_Surname_Name__c = 'TestCase',
                Website__c = 'http://www.test.case',
                Billing_Contact_Email_Address__c = 'TestCase@mymail.com',
                Name_and_Address_of_Auditors__c = 'TestCase',
                Bank__c = 'TestCase',
                Branch_Name_and_Number__c = 'TestCase',
                Account_Name__c = 'TestCase',
                Account_Number__c = 'TestCase',
                Account_Type__c = 'TestCase',
                Name_of_Authorised_Bank_Signatory__c = 'TestCase',
                Street_Name_Number__c = 'TestCase',
                Building_Name__c ='TestCase',
                Floor_and_Room_Number__c = 'TestCase',
                Suburb__c = 'TestCase',
                City_Postal_Code__c = 'TestCase',
                Contact_Name__c = 'TestCase',
                Contact_Fixed_Telephone_Number__c = 'TestCase',
                Contact_Mobile_Number__c = 'TestCase',
                Contact_Email__c = 'TestCase@mymail.com',
                If_answered_Yes_for_such_payment__c = 'TestCase',
                Name__c = 'TestCase',
                Designation__c = 'TestCase',
                A_Government_Official__c = true,
                An_Official_of_an_International_Org__c = true,
                A_Member_of_Political_Party__c = true,
                A_Candidate_for_Gvt_Office__c = true,
                Representative_Employee_of_any_above__c = true,
                A_Relative_of_Any_Above__c= true,
                Improper_Payment_Within_Past_5_yrs__c = true,
                Company__c= true,
                If_answered_Yes_to_any_of_the_above__c = 'TestCase',
                Partnership__c= true,
                Trust__c= true,
                Non_Profit_Association__c= true,
                Other__c= true,
                dat__c = date.today()
            );
                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;
                insert attach;

                List<Attachment> attachments=[select id, name from Attachment where parent.id=:newLead.id];
                System.assertEquals(1, attachments.size());


                test.stopTest(); 

   }
 }
  • first insert lead, then only it will generate id after that insert attachment – Ratan Paul Aug 12 '16 at 12:49
0

Please insert Lead before assigning to attach.

Just now i updated your code please check and let me know

  • thanks @ncmouli I tried that but I am now getting System.DmlException: Insert failed.INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] – user23 Aug 12 '16 at 13:00
  • Please remove see (seeAllData=true) and final attachment query in your code and try againg – ncmouli Aug 12 '16 at 13:15
  • Thanks a lot @ncmouli your assistance is greatly appreciated. – user23 Aug 12 '16 at 13:19
  • Welcome @user23 – ncmouli Aug 12 '16 at 13:23
1

Seems, you have missed to insert Lead record.

First, you need to insert Lead record, then you can use Lead Id to As ParentId for Attachment.

 @isTest (seeAllData=true)
   public class CIFController_test 
   {
    static testMethod void CIFControllerTest()
    {
    test.startTest();
        Lead newLead= new Lead
            (
                lastname = 'TestCase',
                company = 'BrianCp',
                Business_Declaration_Date__c = date.today(),
                CEO__c = 'TestCase',
                CFO__c = 'TestCase',
                HCC__c = 'TestCase',
                CEO_Date__c = date.today(),
                CFO_Date__c = date.today(),
                HCC_Date__c = date.today(),
                Registered_Name__c = 'TestCase',
                Date_Of_Registration__c = date.today(),
                Customer_Name__c = 'TestCase',
                Business_Profile__c = 'TestCase',
                Telecommunications_Licences_if_any__c = 'TestCase',
                Surname_and_First_Name__c = 'TestCase',
                Job_Title__c = 'TestCase',
                Telephone_Number__c = 'TestCase',
                Email__c = 'TestCase@mymail.com',
                Trading_As__c = 'TestCase',
                VAT_Number__c = 'TestCase',
                Registration_Number__c = 'TestCase',
                Registered_Address__c = 'TestCase',
                Business_Address__c = 'TestCase',
                Billing_Contact_Telephone_Number__c = 'TestCase',
                Postal_Address__c = 'TestCase',
                Billing_Address_if_not_same_as_above__c = 'TestCase',
                Billing_Contact_Surname_Name__c = 'TestCase',
                Website__c = 'http://www.test.case',
                Billing_Contact_Email_Address__c = 'TestCase@mymail.com',
                Name_and_Address_of_Auditors__c = 'TestCase',
                Bank__c = 'TestCase',
                Branch_Name_and_Number__c = 'TestCase',
                Account_Name__c = 'TestCase',
                Account_Number__c = 'TestCase',
                Account_Type__c = 'TestCase',
                Name_of_Authorised_Bank_Signatory__c = 'TestCase',
                Street_Name_Number__c = 'TestCase',
                Building_Name__c ='TestCase',
                Floor_and_Room_Number__c = 'TestCase',
                Suburb__c = 'TestCase',
                City_Postal_Code__c = 'TestCase',
                Contact_Name__c = 'TestCase',
                Contact_Fixed_Telephone_Number__c = 'TestCase',
                Contact_Mobile_Number__c = 'TestCase',
                Contact_Email__c = 'TestCase@mymail.com',
                If_answered_Yes_for_such_payment__c = 'TestCase',
                Name__c = 'TestCase',
                Designation__c = 'TestCase',
                A_Government_Official__c = true,
                An_Official_of_an_International_Org__c = true,
                A_Member_of_Political_Party__c = true,
                A_Candidate_for_Gvt_Office__c = true,
                Representative_Employee_of_any_above__c = true,
                A_Relative_of_Any_Above__c= true,
                Improper_Payment_Within_Past_5_yrs__c = true,
                Company__c= true,
                If_answered_Yes_to_any_of_the_above__c = 'TestCase',
                Partnership__c= true,
                Trust__c= true,
                Non_Profit_Association__c= true,
                Other__c= true,
                dat__c = date.today()
            );

            insert newLead; // Insert Lead record

                Attachment attach = new Attachment();     
                attach.Name='Unit Test Attachment';
                Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
                attach.body=bodyBlob;
                attach.parentId=newLead.id;
                insert attach;

                List<Attachment> attachments=[select id, name from Attachment where parent.id=:newLead.id];
                System.assertEquals(1, attachments.size());


                test.stopTest(); 

   }
 }
  • Hi @Devendra thanks for your response, sorry I am still new to Apex/Salesforce where shopuld I insert the Lead Record??? – user23 Aug 12 '16 at 12:52
  • @user23 I have just updated the code in my answer. Added the insert statement. Hope, this should work for you ! – Devendra Aug 12 '16 at 12:54
  • thank you very much I tried that as specified but am getting the error below: System.DmlException: Insert failed.INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] – user23 Aug 12 '16 at 12:59
  • You need to remove the last insert newLead statement. You are inserting an Lead record twice. I have updated the code again. – Devendra Aug 12 '16 at 13:01
  • Thank you very much Devendra.. Your assistance is greatly appreciated – user23 Aug 12 '16 at 13:04
0

Before you insert the lead it does not have an Id, therefore this line:

attach.parentId=newLead.id;

will evaluate to:

attach.parentId=null;

And this gives you the error you are facing. To fix it you should insert the lead before assigning its' Id anywhere

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.