You need to sign in to do that
Don't have an account?
KRay
test class Error System.QueryException: List has no rows for assignment to SObject
Hi everyone, I'm having trouble with a test class. I'm passing a value to the target class variable but when the SOQL query executes, it returns "System.QueryException: List has no rows for assignment to SObject". Please help
Class
TestClass
Even if I update con.CNum in the "testGetAccountStatus" method, I get the same error back, when the test runs. "System.QueryException: List has no rows for assignment to SObject"
Class
global class myClass{ public String AccountName{get;set;} public Account accId{get;set;} public String CNum{get;set;} public VarsityAppInvoice(ApexPages.StandardController cAccount){ accId = (Account)cAccount.getRecord(); CNum = [Select Customer_Number__c FROM Account where id=:accId.id].Customer_Number__c; } public Boolean getAccountStatus(){ AccountName= [SELECT Name FROM Account WHERE Customer_Number__c=:CNum].Name; if(AccountName=null){ return false; } else { return true } } }
TestClass
@isTest global class myClassTest{ static myClass con = new myClass(); public myClass(){ con.CNum='123456'; } @isTest static void testGetAccountStatus(){ Boolean results = con.getAccountStatus(); System.Debug(results); } }
Even if I update con.CNum in the "testGetAccountStatus" method, I get the same error back, when the test runs. "System.QueryException: List has no rows for assignment to SObject"
Hello KRay,
You have two options as suggested by BalajiRanganathan
You can use following code.
Please check your class code codition on line 11 it should be double equal sign.
@isTest
global class myClassTest{
@isTest static void positiveTestGetAccountStatus(){
myClass con = new myClass();
//creating account
Account acc = new Account();
acc.Customer_Number__c = 123456;
acc.Name = 'TestAccount';
insert acc;
Boolean results = con.getAccountStatus();
System.Debug(results);
}
@isTest static void negativeTestGetAccountStatus(){
myClass con = new myClass();
//creating account
Account acc = new Account();
acc.Customer_Number__c = null;
acc.Name = 'TestAccount';
insert acc;
Boolean results = con.getAccountStatus();
System.Debug(results);
}
}
All Answers
Hello KRay,
You have two options as suggested by BalajiRanganathan
You can use following code.
Please check your class code codition on line 11 it should be double equal sign.
@isTest
global class myClassTest{
@isTest static void positiveTestGetAccountStatus(){
myClass con = new myClass();
//creating account
Account acc = new Account();
acc.Customer_Number__c = 123456;
acc.Name = 'TestAccount';
insert acc;
Boolean results = con.getAccountStatus();
System.Debug(results);
}
@isTest static void negativeTestGetAccountStatus(){
myClass con = new myClass();
//creating account
Account acc = new Account();
acc.Customer_Number__c = null;
acc.Name = 'TestAccount';
insert acc;
Boolean results = con.getAccountStatus();
System.Debug(results);
}
}
@RestResource(urlMapping='/Oppty/*')
global with sharing class OpportunitySFDCService{
@HttpPost
global static void UpdateOppMASCustomerID(String MASCustomerID, String OrderSFDCID) {
Order__c orderOppID=[Select Id,Opportunity__c from Order__c where Order_SFDC_ID__c=:OrderSFDCID];
Opportunity opp=new Opportunity(Id=orderOppID.Opportunity__c,MASUniqueId__c=MASCustomerID);
update opp;
}
}