0

I am uploading the salesforce app on app exchange but while uploading it is showing error in test class but in the developer console it is running perfectly and it showing 87% code coverage Error is (Message)

System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [seit__Date_Created__c, seit__Agent_ID__c, seit__Customer_ID__c, seit__Property_ID__c]: [seit__Date_Created__c, seit__Agent_ID__c, seit__Customer_ID__c, seit__Property_ID__c]

My test method is given below

static testMethod void saveA1()
    {
       seit__Invoice_Table__c pro = new seit__Invoice_Table__c(seit__Date_Created__c=System.today(), seit__Agent_ID__c='a0161000004pUjR', seit__Customer_ID__c='a04610000030Nfp', seit__Property_ID__c='a0261000002iTPk',seit__Payment_Mode__c='Instalments',   seit__Instalments__c='3');
       ApexPages.StandardController sc = new ApexPages.standardController(pro);
       InvoiceController controller = new InvoiceController(sc);
       controller.SaveInvoice();


    }

My controller is

    public with sharing class InvoiceController {
         public List<seit__Invoice_Table__c> getInvoice {get; set;}
        public seit__Invoice_Table__c invoice {get; set;}
        public string searchText {get;set;}
        public List<seit__Invoice_Table__c> searchResults {get;set;}

        public InvoiceController(ApexPages.StandardController controller) {
            invoice=new seit__Invoice_Table__c();
            getInvoice = [SELECT Id,Name,seit__Date_Created__c, seit__Agent_ID__c,seit__Agent_Name__c, seit__Discount__c,seit__Customer_Name__c, seit__Property_ID__c,seit__Property_Name__c,seit__Email__c,seit__Customer_Phone_Number__c,seit__Property_Address__c,seit__Property_Size__c,seit__Property_Size_sq_feet__c,seit__Price__c,seit__Agent_Phone_Number_del__c,seit__Payment_Mode__c,seit__Instalments__c,seit__Payment__c,seit__Customer_ID__c  FROM seit__Invoice_Table__c];
        }

        public pageReference SaveInvoice()
        {
            try{
            upsert invoice;  
            PageReference pageRef = new PageReference('https://itroos-dev-ed--seit.na34.visual.force.com/apex/InvoiceDetailImproveVFPage?core.apexpages.request.devconsole=1');
            pageRef.setRedirect(true);
            return pageRef;  
            }
            catch(Exception ex) 
            {
               ApexPages.addMessages(ex);
             system.debug(ex);
             PageReference pageRef = new PageReference('https://itroos-dev-ed--seit.na34.visual.force.com/apex/InvoiceDetailImproveVFPage?core.apexpages.request.devconsole=1');
            pageRef.setRedirect(true); 
            throw ex;
            }
        }
}
3
  • I am not sure why your dev org tests are fine. I think issue is that your pro record is not inserted. Can you insert it and rerun tests? – kurunve Jan 12 '16 at 14:21
  • @kurunve you are right pro is not inserted.... but i dont think there is any problem with the test method **SaveA1() ** – Muhammad Sheheryar Afsar Jan 12 '16 at 14:29
  • @kurunve As i have given all the required fields but it is not inserting any data – Muhammad Sheheryar Afsar Jan 12 '16 at 14:30
0

Add invoice= (seit__Invoice_Table__c )controller.getRecord(); in controller this will solve your issue.

So when you pass record in constructor means standardcontroller having your record.. We need to just get record and assign to controller variable..

So this will solve your problem

 public with sharing class InvoiceController {
        public List<seit__Invoice_Table__c> getInvoice {get; set;}
        public seit__Invoice_Table__c invoice {get; set;}
        public string searchText {get;set;}
        public List<seit__Invoice_Table__c> searchResults {get;set;}

        public InvoiceController(ApexPages.StandardController controller) {
            invoice= (seit__Invoice_Table__c )controller.getRecord();
            getInvoice = [SELECT Id,Name,seit__Date_Created__c, seit__Agent_ID__c,seit__Agent_Name__c, seit__Discount__c,seit__Customer_Name__c, seit__Property_ID__c,seit__Property_Name__c,seit__Email__c,seit__Customer_Phone_Number__c,seit__Property_Address__c,seit__Property_Size__c,seit__Property_Size_sq_feet__c,seit__Price__c,seit__Agent_Phone_Number_del__c,seit__Payment_Mode__c,seit__Instalments__c,seit__Payment__c,seit__Customer_ID__c  FROM seit__Invoice_Table__c];
        }

        public pageReference SaveInvoice()
        {
            try{
            upsert invoice;  
            PageReference pageRef = new PageReference('https://itroos-dev-ed--seit.na34.visual.force.com/apex/InvoiceDetailImproveVFPage?core.apexpages.request.devconsole=1');
            pageRef.setRedirect(true);
            return pageRef;  
            }
            catch(Exception ex) 
            {
               ApexPages.addMessages(ex);
             system.debug(ex);
             PageReference pageRef = new PageReference('https://itroos-dev-ed--seit.na34.visual.force.com/apex/InvoiceDetailImproveVFPage?core.apexpages.request.devconsole=1');
            pageRef.setRedirect(true); 
            throw ex;
            }
        }
}
3
  • Thanks @Ratan it worked.I want to know one more thing that in test classes we have to write code for the both scenarios means in one method we successfully insert the data and in the other we fail but in failing the test method doesnot execute and give the error Required Fields Are Missing......How can we overcome this and write a successfulll test code – Muhammad Sheheryar Afsar Jan 12 '16 at 17:54
  • @Sheheri check this post salesforce.stackexchange.com/questions/100711/… how to cover catch block... – Ratan Paul Jan 12 '16 at 17:58
  • 1
    @Sheheri ans to your question.. in your code catch block if you remove the throw ex; then this will work.. you will not get any error.. The reason I am saying because you already handled the exception using try catch.. Good night time for bed.. – Ratan Paul Jan 12 '16 at 18:00

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.