Below is my controller class.

Public class VMSalesOrderUserEditpage{
public list<VM_Sales_Order__c> vmList{get;set;}
public VM_Sales_Order__c VMSOrd {get;set;}
public string vmId {get;set;}

    public VMSalesOrderUserEditpage(ApexPages.StandardController controller) {
    vmId = apexpages.currentpage().getparameters().get('id');
    VMSOrd =[select Shipping_Address__c from VM_Sales_Order__c where id=: vmId ];
//vmList= new List<VM_Sales_Order__c>();
if(VMSOrd !=NULL){
vmList = [select id,name,Shipping_Address__c,Customer_Account__c,VM_Sales_Order__c.Shipping_Address__r.name,Lab_Id__c,Order_Date__c,Order_Status__c,CurrencyIsoCode,Sales_Order_Amount__c,Cell_Sep_Product_Count__c,
                                       Contact_Name__c, Customer_PO__c,Contact_Email__c,Principle_Investigator__c,Contact_Phone__c
                                        from VM_Sales_Order__c where Shipping_Address__c =:VMSOrd.Shipping_Address__c];


My Test Class

//Inserting an Account

            RecordType RT = [select id from RecordType where name ='Location' and sobjecttype = 'Territory_Component__c']; 
            RecordType RT_cust = [select id from RecordType where name ='Customer' and sobjecttype = 'Territory_Component__c']; 
            RecordType RT_export = [select id from RecordType where name ='Export' and sobjecttype = 'Territory_Component__c']; 

            List<account> accountsToInsert = new List<account>();
            Set<String> billingCountries = new Set<String>();

            System.runAs(u) { //only admins setup mappings
                Territory__c territory1 = Test_CustomObjectUtil.createTerritory('USCASTEST',false);
                insert territory1;
                Territory_Component__c territoryComponent1 = Test_CustomObjectUtil.createTerritoryComponentLocation(territory1.Id, RT.Id, 'UNITED STATES', 'CA','9999',false);    
                insert territoryComponent1;

                //Test retrieval of reps for one territory
                Sales_Territory_Mapping__c stmFSALM = Test_CustomObjectUtil.createSalesTerritoryMapping(u.Id, 'FSALM', territory1.Id);
                Sales_Territory_Mapping__c stmFSALCS = Test_CustomObjectUtil.createSalesTerritoryMapping(u.Id, 'FSALCS', territory1.Id);
                stmFSALCS.Account_Owner__c = true;
                update stmFSALCS;        


                Territory__c territory2 = Test_CustomObjectUtil.createTerritory('FRNORTHTEST',false);
                insert territory2;
                Territory_Component__c territoryComponent2 = Test_CustomObjectUtil.createTerritoryComponentLocation(territory2.Id, RT.Id, 'BELGIUM', 'XX','A',false);   //test trim 1 
                insert territoryComponent2;

                 Account a1 = Test_StandardObjectUtil.createAccount('1', false);
               a1.BillingCountry = 'UNITED STATES';
               a1.BillingCity = 'Los Angeles';
               a1.BillingState = 'CA';
               a1.BillingPostalCode = '9999999';
               a1.Type = 'Acadamic';
               a1.OwnerId =u.id;
               Insert a1;

          //     accountsToInsert.add(a1);
          //     billingCountries.add(a1.BillingCountry);

    //Inserting an VMSales Order

    Account acct = [SELECT Id FROM Account where name ='TestSFDC123'];

    List <VM_Sales_Order__c> vmsaleslist = new List <VM_Sales_Order__c>();

    VM_Sales_Order__c VMSO = New VM_Sales_Order__c();
    VMSO.Name ='TestVMSO';
    VMSO.Customer_Account__c = acct.Id;
    VMSO.VM_Sales_Order_Id__c = 'T363772';
    Insert VMSO;     

    ApexPages.StandardController sc = new ApexPages.standardController(VMSO);
    VMSalesOrderUserEditpage callClass = new VMSalesOrderUserEditpage(sc);

The test class fails and displays an error message saying "List has no rows for assignment to SObject". I am not sure if the way I called the constructor from the class is correct. Can someone suggest me where am i going wrong.

  • Without an asserts there is no meaning to test classes. You have 2 problems, assigning a query to an Object and second is not validating using asserts if records are inserted properly.
    – Raul
    Oct 18 '17 at 16:47

You didn't specify the Id page parameter:

ApexPages.currentPage().getParameters().put('id', VMSO.Id);

I strongly recommend that you don't do this, but simply change your constructor:

VMSOrd =[select Shipping_Address__c from VM_Sales_Order__c where id=:controller.getId()];

Note that the following code is irrelevant:

if(VMSOrd !=NULL){

If no valid ID is specified, you will get a QueryException. If you intend to allow the page to work with new records as well as existing records, use the record directly from the controller:

VMSOrd = (VM_Sales_Order__c)controller.getRecord();

Your Answer

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