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
etechcareersetechcareers 

Error on Required Field in Unit Testclass????

Hi all:

   I really need help...

in my Apex Controller, I have:

 

    public PageReference addmore2() {
     
    //Payment Detail
    pd.Donation__c = ApexPages.currentPage().getParameters().get('oppid');
 insert pd;
}

 So as you can see I am calling Donation__c which is a required field for Payment DETAIL.

It works but in my unit test class I get:

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

 

 

 

static testMethod void testaddingmore2payments() {
    ZnewOpportunityController ext = new ZnewOpportunityController();

    Account acct2 = ext.getAccount();
    acct2.name='scde';
    acct2.Org_Recognition_Name__c='UFCdd';
    acct2.Type='Individual';
    acct2.Relationship_Stage__c='Lead';
    acct2.BillingState='CT';
    acct2.BillingStreet='134 Laurel';
    acct2.BillingCity='Mineola';
    acct2.BillingPostalCode='11501';
    acct2.BillingCountry='USA';
    acct2.ShippingState = 'CT';
    acct2.ShippingCountry = 'USA';
    acct2.ShippingPostalCode = '11501';
    acct2.ShippingCity = 'Mineola';
    acct2.ShippingStreet = '134 Laurel';
    acct2.recordtypeid = '012700000005UJ9AAM';
    
    Contact con2 = ext.getContact();
    con2.AccountId = acct2.Id;
    con2.firstname='jdhjdss';
    con2.lastname='hdjdhdhzz';
    con2.email='hellozz@email.com';
    con2.phone='5161111111';
    con2.recordtypeid = '012700000005ITPAA2';    
    
    Opportunity opp2 = ext.getOpportunity();
    opp2.Name = 'lslslsdsaaa';
    opp2.Accountid = acct2.id;
    opp2.stageName = 'Lead';
    opp2.CloseDate = Date.newinstance(2010,12,31);
    opp2.Acknowledge_Type__c = 'City';
    opp2.Funding_Source__c = 'Individual';
    opp2.Public_Private_Funding__c = 'Public';
    opp2.StageName = 'Prospect';
    opp2.Reinvestment__c = 'Reinvestment';
    opp2.FYs_Applied_To__c = 'FY11'; 
    opp2.recordtypeid ='012700000000vQxAAI';  
    
    Payment_Detail__c pmt2 = ext.getPaymentDetail();        
    pmt2.Donation__c = opp2.Id;
    pmt2.Applies_To_FY__c = 'FY11';
    pmt2.Date_Due__c = Date.newinstance(2010,12,31);
    pmt2.Amount_Due__c =100;
    pmt2.Date_Received__c = Date.newinstance(2010,12,31);
    pmt2.Amount_Paid__c = 100;
    pmt2.Check_Date__c=Date.newinstance(2010,12,31);
    pmt2.Check_Number__c='112';
    pmt2.Check_Bank__c='hehehe';
    pmt2.recordtypeid = '012700000009PxrAAE';
  
    
    Allocation__c alloc2 = ext.getAllocation ();    
    alloc2.Donation__c = opp2.id;
    alloc2.Payment_Detail__c = pmt2.id;
    alloc2.Amount_allocated__c = 100;
    alloc2.FY_Allocated_To__c = 'FY11';
    alloc2.Program_Location__c = 'Chicago'; 
    
    
    ext.addmore2();
  }

 

Please any any advice would help...

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

Put the id in addmore2() method otherwise pd.Donation__c returns null when you call this method in testmethod :

 

             Opportunity opp = new Opportunity(name = "testopp");

             insert opp;

            ApexPages.CurrentPage().getParameters().put('id', opp.Id);

            ApexPages.StandardController controller1 = new ApexPages.StandardController(opp);

            ZnewOpportunityController  ext = new ZnewOpportunityController(controller1);   

 

Hope this helps.            

All Answers

SurekaSureka

Hi,

 

I guess, this error could be due to the calling of "addmore"  method, since you are trying to get "oppid" which will obviously not return any value when it is called via test method. Thats why, eventhough you are assigning "pmt2.Donation__c = opp2.Id;", it will be null when it is calling addmore  method.

 

That is the reason you are getting the error.

 

Thanks

bob_buzzardbob_buzzard

Your addMore2() method is trying to pull a parameter of oppid from the URL, but you haven't provided that as part of your test so it is null.

 

You'll need to insert an opportunity and then set up the parameter something like the following prior to calling ext.addMore2:

 

 

insert opp2;
ApexPages.currentPage().getParameters().put('oppid', opp2.id);

 

 

 

 

 

 

Pradeep_NavatarPradeep_Navatar

Put the id in addmore2() method otherwise pd.Donation__c returns null when you call this method in testmethod :

 

             Opportunity opp = new Opportunity(name = "testopp");

             insert opp;

            ApexPages.CurrentPage().getParameters().put('id', opp.Id);

            ApexPages.StandardController controller1 = new ApexPages.StandardController(opp);

            ZnewOpportunityController  ext = new ZnewOpportunityController(controller1);   

 

Hope this helps.            

This was selected as the best answer