+ Start a Discussion
lovetolearnlovetolearn 

Help with Unit Test

Hi,

 

I am having some trouble writing unit test for my apex class. I keep getting the error:System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Member__c]: [Member__c]. 

 

Here is my code: 

 

public class exNewFeedback {

    public Case requests {get; set;}
    public String details {get; set;}
    public String fbFrom {get; set;}
    public String fbSource {get; set;}
  
    
    public exNewFeedback(ApexPages.StandardController controller) {
        this.requests = (Case)controller.getRecord();
        this.requests = [SELECT ID, accountID, Supplier__c, User_Owner_Office__c FROM Case WHERE ID =: requests.ID][0];
    }
           
    public PageReference createRequest(){
        Feedback__c compliment = new Feedback__c();
        compliment.Member__c = requests.accountID;
        compliment.Request__c = requests.ID;
        compliment.Supplier__c = requests.Supplier__c;
        compliment.Escalated_to_Complaint__c = 'No';
        compliment.Relevant_office__c = requests.User_Owner_Office__c;
        compliment.Details__c = details;
        compliment.Feedback_From__c = fbFrom;
        compliment.Feedback_Source__c=fbSource;
        insert compliment;
     
        PageReference fbPage = new ApexPages.StandardController(compliment).view();
        fbPage.setRedirect(true);
        return fbPage;
    }
    
    static testMethod void testNewFeedback(){
           Account member = new Account(Name = 'Test User');
           insert member;
           LIST<Account> memberlist = [SELECT ID FROM Account LIMIT 1];
           System.assertEquals(1, memberlist.size());
           Contact c = new Contact(FirstName = 'Test', LastName='Test', Account = member, Region__c='Africa', 
                                   Newsletter_Preferences_taken_setup__c = 'Yes', Where_did_client_hear_of_us__c = 'Do Not Know',
                                   Membership_Status__c = 'N/a - Corporate Bus Dev Contact');
           insert c;
           List<Contact> clist = [SELECT ID FROM CONTACT LIMIT 1];
           System.AssertEquals(1, clist.size());
           Case request = new Case(Contact = c, Origin = 'Email', Status = 'Open');
           insert request;
           List<Case> requestlist = [SELECT ID FROM CASE LIMIT 1];
           System.AssertEquals(1, requestlist.size());
           Case requests = [SELECT ID, accountID, Supplier__c, User_Owner_Office__c FROM CASE WHERE 
                            ID =: request.ID];
           Apexpages.Standardcontroller stdCon = new apexpages.Standardcontroller(requests);
           exNewFeedback exFB = new exNewFeedback (stdCon);
           exFB.createRequest();   
    }
}

 Please let me know where I went wrong. Thank you. 

Best Answer chosen by Admin (Salesforce Developers) 
ShamilShamil

It seems like you're not populating the AccountId for the request Case:

 

Case request = new Case(Contact = c, Origin = 'Email', Status = 'Open');

Change that to 

Case request = new Case(AccountId = member.Id, Contact = c, Origin = 'Email', Status = 'Open');

 

 

 

All Answers

ShamilShamil

It seems like you're not populating the AccountId for the request Case:

 

Case request = new Case(Contact = c, Origin = 'Email', Status = 'Open');

Change that to 

Case request = new Case(AccountId = member.Id, Contact = c, Origin = 'Email', Status = 'Open');

 

 

 

This was selected as the best answer
lovetolearnlovetolearn

Thank you!!