You need to sign in to do that
Don't have an account?
Andrew Aldis
System.QueryException: List has no rows for assignment to SObject on test class
I am trying to write a test class for a controller extension and cannot seem to get it to work I keep getting a error: "System.QueryException: List has no rows for assignment to SObject on test class" and cannot figure out why. My controller and test class are below.
public class HardwareWoSalesOrderLineEdit {
public HardwareWoSalesOrderLineEdit(ApexPages.StandardController controller) {
//parent record
this.proj =[select Sales_Order__c, Id, X2nd_Sales_Order__c From Work_Order__c Where Id = :ApexPages.currentPage().getParameters().get('id')];
// child records
this.line = [ SELECT SCMC__Quantity__c, Item_Name__c,SCMC__Quantity_Shipped__c, Category__c, SCMC__Item_Description__c, PM_Ordering_Details__c, Quantity_to_Ship__c, Remaining_Quantity__c
FROM SCMC__Sales_Order_Line_Item__c
WHERE (Item_Type__c = 'Hardware' AND SCMC__Status__c != 'Completed' AND SCMC__Status__c != 'Shipped' AND Category__c != 'Physical Install' AND SCMC__Status__c != 'Cancelled' AND SCMC__Sales_Order__c = :proj.Sales_Order__c) OR
(Item_Type__c = 'Hardware' AND SCMC__Status__c != 'Completed' AND SCMC__Status__c != 'Shipped' AND Category__c != 'Physical Install' AND SCMC__Status__c != 'Cancelled' AND SCMC__Sales_Order__c = :proj.X2nd_Sales_Order__c) ORDER BY Category__c ASC ];
}
//get sales order lines
public SCMC__Sales_Order_Line_Item__c[] getline() {
return this.line;
}
// save method to save changes to the sales order lines
public pagereference saveChanges() {
upsert this.line;
pageRef.setRedirect(true);
return pageRef;
}
// class variables
public PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
public Work_Order__c proj;
public SCMC__Sales_Order_Line_Item__c[] line;
}
TEST CLASS
@istest(seealldata=true)
public class HardwareWoSalesOrderLineEditTest{
static testMethod void HardwareWoSalesOrderLineEditTest(){
Test.startTest();
SCMC__Sales_Order_Line_Item__c sol = [Select Id, SCMC__Item_Master__c, SCMC__Sales_Order__c, SCMC__Sales_Order__r.Installation__c, CreatedById,
SCMC__Sales_Order__r.SCMC__Customer_Account__c
from SCMC__Sales_Order_Line_Item__c where Item_Type__c = 'Hardware' AND SCMC__Status__c != 'Completed'
AND SCMC__Status__c != 'Shipped' AND Category__c != 'Physical Install' limit 1];
system.assertnotEquals(null, sol);
system.debug(sol.Id);
Work_Order__c wo = new Work_Order__c(
Account__c = sol.SCMC__Sales_Order__r.SCMC__Customer_Account__c,
Sales_Order__c = sol.SCMC__Sales_Order__c,
Installation__c = sol.SCMC__Sales_Order__r.Installation__c,
Resource__c = sol.CreatedById,
Type__c = 'Hardware Order',
Start_Date__c = system.today(),
Due_Date__c = system.today(),
Confirm_address_is_correct__c = true,
RecordTypeId = '01234000000Bmk8',
Name = 'testwo123');{
insert wo;}
system.assertnotEquals(null, wo);
system.debug(wo.Id);
Work_Order__c w = [Select Id from Work_Order__c Where Name = 'testwo123' Limit 1];
system.assertnotEquals(null, w);
system.debug(w);
Test.setCurrentPage(Page.HardwareWoSalesOrderLineEdit);
ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(w);
system.debug(sc);
HardwareWoSalesOrderLineEdit myPageCon = new HardwareWoSalesOrderLineEdit(sc);
system.debug(myPageCon);
myPageCon.getline().add(sol);
myPageCon.saveChanges();
Test.stopTest();
}
}
public class HardwareWoSalesOrderLineEdit {
public HardwareWoSalesOrderLineEdit(ApexPages.StandardController controller) {
//parent record
this.proj =[select Sales_Order__c, Id, X2nd_Sales_Order__c From Work_Order__c Where Id = :ApexPages.currentPage().getParameters().get('id')];
// child records
this.line = [ SELECT SCMC__Quantity__c, Item_Name__c,SCMC__Quantity_Shipped__c, Category__c, SCMC__Item_Description__c, PM_Ordering_Details__c, Quantity_to_Ship__c, Remaining_Quantity__c
FROM SCMC__Sales_Order_Line_Item__c
WHERE (Item_Type__c = 'Hardware' AND SCMC__Status__c != 'Completed' AND SCMC__Status__c != 'Shipped' AND Category__c != 'Physical Install' AND SCMC__Status__c != 'Cancelled' AND SCMC__Sales_Order__c = :proj.Sales_Order__c) OR
(Item_Type__c = 'Hardware' AND SCMC__Status__c != 'Completed' AND SCMC__Status__c != 'Shipped' AND Category__c != 'Physical Install' AND SCMC__Status__c != 'Cancelled' AND SCMC__Sales_Order__c = :proj.X2nd_Sales_Order__c) ORDER BY Category__c ASC ];
}
//get sales order lines
public SCMC__Sales_Order_Line_Item__c[] getline() {
return this.line;
}
// save method to save changes to the sales order lines
public pagereference saveChanges() {
upsert this.line;
pageRef.setRedirect(true);
return pageRef;
}
// class variables
public PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
public Work_Order__c proj;
public SCMC__Sales_Order_Line_Item__c[] line;
}
TEST CLASS
@istest(seealldata=true)
public class HardwareWoSalesOrderLineEditTest{
static testMethod void HardwareWoSalesOrderLineEditTest(){
Test.startTest();
SCMC__Sales_Order_Line_Item__c sol = [Select Id, SCMC__Item_Master__c, SCMC__Sales_Order__c, SCMC__Sales_Order__r.Installation__c, CreatedById,
SCMC__Sales_Order__r.SCMC__Customer_Account__c
from SCMC__Sales_Order_Line_Item__c where Item_Type__c = 'Hardware' AND SCMC__Status__c != 'Completed'
AND SCMC__Status__c != 'Shipped' AND Category__c != 'Physical Install' limit 1];
system.assertnotEquals(null, sol);
system.debug(sol.Id);
Work_Order__c wo = new Work_Order__c(
Account__c = sol.SCMC__Sales_Order__r.SCMC__Customer_Account__c,
Sales_Order__c = sol.SCMC__Sales_Order__c,
Installation__c = sol.SCMC__Sales_Order__r.Installation__c,
Resource__c = sol.CreatedById,
Type__c = 'Hardware Order',
Start_Date__c = system.today(),
Due_Date__c = system.today(),
Confirm_address_is_correct__c = true,
RecordTypeId = '01234000000Bmk8',
Name = 'testwo123');{
insert wo;}
system.assertnotEquals(null, wo);
system.debug(wo.Id);
Work_Order__c w = [Select Id from Work_Order__c Where Name = 'testwo123' Limit 1];
system.assertnotEquals(null, w);
system.debug(w);
Test.setCurrentPage(Page.HardwareWoSalesOrderLineEdit);
ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(w);
system.debug(sc);
HardwareWoSalesOrderLineEdit myPageCon = new HardwareWoSalesOrderLineEdit(sc);
system.debug(myPageCon);
myPageCon.getline().add(sol);
myPageCon.saveChanges();
Test.stopTest();
}
}
Hi Andrew,
Could you please try to put queries on developer console - Query editor
1.
2.
Please let me know the results
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Hi Andrew,
The exception is because of you are not getting any record in your SOQL query..
which meet the where criteria so it's give error.
This error occurs when query doesn't return any rows.
Follow the below link, it would be helpful for you.
https://help.salesforce.com/articleView?id=000159853&type=1
Thanks,
Arvind Kumar
Do you think you can not create records for SCMC__Sales_Order_Line_Item__c object in test class .If you can then create records in test class .Basically @isTest(SeeAllData=true) we should use only for those records which we can not create in test environment .
If your organisation don't have records for this object then it will give you error .
It is not best practice to handle exception in test class .
Hope this help you to solve your problem .
Thanks
Manoj
@Andrew, Have you checked these two queries on developer console ? Did you get result ?
I am having doubt on this query -
SCMC__Sales_Order_Line_Item__c sol = [Select Id, SCMC__Item_Master__c, SCMC__Sales_Order__c, SCMC__Sales_Order__r.Installation__c, CreatedById,
SCMC__Sales_Order__r.SCMC__Customer_Account__c
from SCMC__Sales_Order_Line_Item__c where Item_Type__c = 'Hardware' AND SCMC__Status__c != 'Completed'
AND SCMC__Status__c != 'Shipped' AND Category__c != 'Physical Install' limit 1];
As this query doesn't return any records, Please let me know the same :)
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com